0

I have two text files. Using sed or grep, how can i find matching string and then replace the string from text2.txt?

It should search anything under name=" ". If found, replace it what is under path=" ". output.txt has desired output data.

test1.txt

domain merge_requests
abc frameworks merge_requests
dvc frame-test merge_requests

test2.txt

<project path="test/domain" name="device/common" />
<project path="test1/frameworks " name="test/frameworks" />
<name="test/frame-test" project path="test3/frame-test" />

output.txt

test/domain merge_requests
test1/frameworks merge_requests
test/frame-test merge_requests
tipu
  • 1
  • nearly same homework: http://unix.stackexchange.com/questions/311113/replacing-substring-in-file-a-with-string-in-file-b-when-a-match-is-found – Ipor Sircer Sep 20 '16 at 17:10
  • @IporSircer I don't see the relationship between this question and the one you've referenced – Chris Davies Sep 20 '16 at 21:45
  • @IporSircer, why do you think it's homework? – ilkkachu Sep 20 '16 at 21:59
  • 2
    @tipu, does it have to be specifically sed or grep? What's the point of the abc and dvc, they seem to be gone from the output (but I can't tell why). Also, is the final line of test2.txt correct? – ilkkachu Sep 20 '16 at 22:05
  • The third line in test2.txt does not seems to fit the same XML ans the other lines (invalid as the element name is after the attribute) – рüффп Jan 19 '17 at 14:58

1 Answers1

0

Your description does something else than your example. Following your example, you always want to replace the substring after the / from the first quoted string by the whole first quoted string, removing a preceeding word, if present.

So you could first collect the first quoted strings from test2.txt in the hold buffer like this:

sed 's/[^"]*"\([^"]*\)".*/\1/;H;$!d;g' test2.txt

Then use this collection for the replacement by appending it to each hold:

sed '/<.*>/{s/[^"]*"\([^"]*\)".*/\1/;H;d};G;s/[^ ]* *\([^ ]\{1,\}\)\( [^\n]*\)\n.*\n\([a-z0-9]*\/\)\1.*/\3\1\2/;s/\n.*//' test2.txt test1.txt

The final s command is to take care of lines without match; you can leave that away if this won't happen in your case. This gives your output.

If you want it more like you described it, you can adapt it. But beware of the changing order of name and path.

Philippos
  • 13,453