Prepare text in file

I used the awk command to find a specific line in a file and would like to add it to the second file. Can anyone help me in this regard?

+4
source share
4 answers

The short answer is that you cannot. You will need a temporary file.

echo "Prepended Line" > tmpfile && cat origfile >> tmpfile && mv tmpfile origfile 

Edit:

 sed -i 's/\(line you want\)/Prefix \1/g' origfile 
+11
source

I would cat(1) line in a dummy file, cat second file after it, and then overwrite the second file with the dummy. Take a look at the sponge(1) command, which allows you to execute a "natural" (but incorrect) awk ... | cat - second-file > second-file awk ... | cat - second-file > second-file

0
source
 awk '{if(NR==1){print "text" $0}else{print }}' O/Pfile temp mv temp O/Pfile 
0
source

Different ways to add a string:

 (echo 'line to prepend';cat file)|sponge file sed -i '1iline to prepend' file # GNU sed -i '' $'1i\\\nline to prepend\n' file # BSD printf %s\\n 0a 'line to prepend' . w|ed -s file perl -pi -e 'print"line to prepend\n"if$.==1' file 

Different ways to add a file:

 cat file_to_prepend file|sponge file { rm file;cat file_to_prepend ->file; }<file sed -i '1{h;s/.*/cat file_to_prepend/ep;g}' file # GNU printf %s\\n '0r file_to_prepend' w|ed -s file sed -i -e 1rfile_to_prepend -e '1{h;d}' -e '2{x;G}' file 
0
source

Source: https://habr.com/ru/post/1341757/


All Articles