0

a.txt has only one long string as example:

echo 'stack=log1:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu1:LOGEMU' >a.txt

I would like insert 2 lines after string stack=log1:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu1:LOGEMU in a.txt as below :

~$ cat a.txt
    stack=log1:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu1:LOGEMU
#insert after above string
    stack=log2:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu2:LOGEMU
    stack=log3:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu3:LOGEMU

I tried

sed '/stack\=log1:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu1:LOGEMU/a stack=log2:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu2:LOGEMU' a.txt

but failed.

What's the problem?

oguz ismail
  • 1
  • 16
  • 47
  • 69
kittygirl
  • 2,255
  • 5
  • 24
  • 52
  • 1
    Could you please do mention on where to insert the line, if you could elaborate on condition part more it will make question more clear, thank you. – RavinderSingh13 Jun 18 '21 at 17:53
  • Are you trying to append 2 lines to `a.txt` no matter what it contains (and you're telling us it only contains 1 line, the target line) or are you trying to append 2 lines after the target line wherever that target line appears in `a.txt`? Please [edit] your question provide more truly representative sample input/output that unambiguously demonstrates your requirements. – Ed Morton Jun 18 '21 at 18:25

3 Answers3

2

You can use it like this:

sed -i.bak '/^stack=log1:/r b.txt' a.txt

cat a.txt

stack=log1:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu1:LOGEMU
stack=log2:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu2:LOGEMU
stack=log3:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu3:LOGEMU

Where you have:

cat b.txt

stack=log2:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu2:LOGEMU
stack=log3:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu3:LOGEMU

Details:

  • /^stack=log1:/: Search for string stack=log1: at the start a line
  • r b.txt: Insert content from file b.txt after the match
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

Your question isn't clear. If a.txt only has 1 line and you want to add those 2 other lines to that file then all you need is:

cat <<! >> a.txt
stack=log2:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu2:LOGEMU
stack=log3:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu3:LOGEMU
!

Otherwise if a.txt is a multi-line file and you want to add 2 lines after your target line no matter where it appears in that file then:

awk '
{ print }
$0 == "stack=log1:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu1:LOGEMU" {
    print "stack=log2:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu2:LOGEMU"
    print "stack=log3:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu3:LOGEMU"
}
' a.txt

To update a.txt if you have GNU awk use awk -i inplace 'script' a.txt, or with any awk tmp=$(mktemp) && awk 'script' a.txt > "$tmp" && mv "$tmp" a.txt.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • I tried method with multi lines,it seemed not workable when `sudo awk ' { print }...`. – kittygirl Jun 18 '21 at 18:28
  • It's workable just fine. If you have a specific problem please tell me some symptoms so I can help. – Ed Morton Jun 18 '21 at 19:09
  • When `sudo awk ' { print }...'a.txt`,just print the output,`a.txt` has no any change. – kittygirl Jun 19 '21 at 07:21
  • How about without the `sudo`? Maybe you have DOS line endings (see https://stackoverflow.com/questions/45772525/why-does-my-tool-output-overwrite-itself-and-how-do-i-fix-it)? Maybe you have white space at the end of lines? As you can see in my answer when you run the script I posted against the input file you posted it does produce the expected output you posted so if it's not doing the same for you then either a) you copy/pasted the script incorrectly or b) the input file you're running it against is different in some way from the input file you posted? – Ed Morton Jun 19 '21 at 13:00
2

Insert two lines after a match should work:

sed -i '/^stack=log1/a \
stack=log2:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu2:LOGEMU \
stack=log3:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu3:LOGEMU' a.txt
GAD3R
  • 4,317
  • 1
  • 23
  • 34