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.


domain merge_requests
abc frameworks merge_requests
dvc frame-test merge_requests


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


test/domain merge_requests
test1/frameworks merge_requests
test/frame-test merge_requests
  • nearly same homework: unix.stackexchange.com/questions/311113/… Commented Sep 20, 2016 at 17:10
  • @IporSircer I don't see the relationship between this question and the one you've referenced Commented Sep 20, 2016 at 21:45
  • @IporSircer, why do you think it's homework?
    – ilkkachu
    Commented Sep 20, 2016 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
    Commented Sep 20, 2016 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)
    – рüффп
    Commented Jan 19, 2017 at 14:58

1 Answer 1


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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .