How can I insert a set of lines (about 5) into a file at the first place a string is found?
For example:
BestAnimals.txt
dog
An awk solution:
awk '/cat/ && c == 0 {c = 1; print "giraffe"}; {print}' \
BestAnimals.txt
If the animals you want to insert are in "MyOtherBestAnimals.txt" you can also do
awk '/cat/ && c == 0 {c = 1; system("cat MyOtherBestAnimals.txt") }; {print} ' \
BestAnimals.txt
This answer can basically be broken down as follows, because ;
separates the awk condition-action pairs:
/cat/ && c == 0 { c = 1; ... }
sets c
to 1 at the first row containing cat. The commands put at the ...
are then executed, but only once, because c
is 1 now.{print}
is the action print with no condition: prints any input line. This is done after the above condition-action pair.Depending on what is actually at the ...
, giraffe is printed, or the contents of "MyOtherBestAnimals.txt" is sent to the standard output, before printing the first line containing "cat".
Edit
After analysis of @glenn jackman's solution, it seems this solution can still be improved: when using input file
nyan cat
cat
the data is appended before nyan cat
and not before the line equal to cat
. The solution is then to request the full line to be equal to cat
:
awk '$0 == "cat" && c == 0 {c = 1; print "giraffe"}; {print}' \
BestAnimals.txt
for the insertion of a single line and
awk '$0 == "cat" && c == 0 {c = 1; system("cat MyOtherBestAnimals.txt") }; {print} ' \
BestAnimals.txt
for the insertion of a file