I would like to insert lines into a file in bash starting from a specific line.
Each line is a string which is an element of an array
line[0]="foo"
line[1]="bar"
...
and the specific line is ‘fields’
file="$(cat $myfile)"
for p in $file; do
if [ "$p" = 'fields' ]
then insertlines() #<- here
fi
done
This can be done with sed:
sed 's/fields/fields\nNew Inserted Line/'Use
-ito save in-place instead of printing tostdoutsed -i 's/fields/fields\nNew Inserted Line/'As a bash script: