With awk
:
awk '1;/PATTERN/{ print "add one line"; print "\\and one more"}' infile
Keep in mind that some characters can not be included literally so one has to use escape sequences (they begin with a backslash) e.g. to print a literal backslash one has to write \\
.
It's actually the same with sed
but in addition each embedded newline in the text has to be preceded by a backslash:
sed '/PATTERN/a\
add one line\
\\and one more' infile
For more details on escape sequences consult the manual.
Also, to address some of the comments: the above commands DO NOT edit the file in place, they just print the result to the standard output. To actually modify the input file you would either use the -i
switch if your awk
/sed
support it (consult the manual) or redirect to a temporary file then overwrite the original e.g.
cmd infile > outfile
mv outfile infile
Or use ed
/ex
which can edit the files in-place on all platforms:
ex -s infile <<\IN
/PATTERN/a
add one line
and one more
.
w
q
IN
Remember: with ed
/sed
/ex
, a
appends and i
inserts; with awk
, to insert, move the 1
to the end.
input
and theoutput
lines. Because you Q is unclear. You could also doecho "Hello World [option]" >> file.txt
, but it doesn't make sense.