as it adds a name below a specific search

Asked

Viewed 27 times

0

I have a file with the name lista.txt

cat lista.txt
nome1
nome2
nome3

now I have searched the name with specific awk

awk '/nome2/' lista.txt
nome2

awk brings me the name I seek!!

now I want to add the name test below the one I sought!

the end result I want gets like this!

cat lista.txt
nome1
nome2
teste
nome3

know that it is possible in other ways as the use of sed for example!

the more I want to do it with awk

1 answer

0


You can do the following:

$ awk '1; /nome2/ {print "teste"}' arquivo.txt
nome1
nome2
teste
nome3
  • 1; prints all lines.
  • /nome2/ {print "teste"} prints "test" when you find "Nome2".
  • thank you very much! I had tried this before but put the 1 after the print action ! I realized that print adds the test name with a line break n ! this and pattern? I thought it would put aside !

  • @socketplus without line break is with printf.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.