0

I believe i'm pretty close but there is 1 command missing..

i have file1.txt

rrrrrrrrrrrrrrrrr
dddddddddddddddddd
id="8" efwef
aaaaaaaaaaaaaaaaaaa
cccccccccccccccccccc
xxxxxxxxxxxxxxxxxxxxx

file2.txt

123

123

what i need is :

rrrrrrrrrrrrrrrrr
dddddddddddddddddd
123

123
cccccccccccccccccccc
xxxxxxxxxxxxxxxxxxxxx

explained: find pattern (id="8") delete pattern line +1 insert into file1.txt content of file2.txt

i got this sed command

sed -e '/id="8"/,+1{' -e 'r /tmp/file2.txt' -e 'd' -e '}' -i /tmp/file1.txt

it does all i need ... except it inserts file2.txt twice...

probably because the ,+1 but i tryed several combos but can't get to it

0

Instead of trying to specify the next line via an address range, try using N to pull it into the pattern space before the d:

$ sed -e '/id="8"/{r file2.txt' -e 'N;d;}' file1.txt
rrrrrrrrrrrrrrrrr
dddddddddddddddddd
123

123
cccccccccccccccccccc
xxxxxxxxxxxxxxxxxxxxx
  • strangely it does no change the file ... i added a >/tmp/newfile.txt to be sure and it is equal to file 1...no change – Lcross Portugal Oct 7 '18 at 22:13
  • what the ^means ? i removed it and it seems to work fine – Lcross Portugal Oct 7 '18 at 22:16
  • @LcrossPortugal the ^ anchors the regex to the start of the line (to prevent false matches elsewhere in a line) - if your input is not as shown in your question (i.e. has other characters - such as leading spaces) then yes, remove it - I will do so in the answer as well – steeldriver Oct 7 '18 at 22:20
  • thanks ... indeed i had some text before the pattern... marked as correct as it should ! thanks – Lcross Portugal Oct 7 '18 at 22:25
  • Just a small detail .. it doesn't put in new line 123cccccccccccccccccccc any tricks for that – Lcross Portugal Oct 7 '18 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.