Bash script - add/edit to file and save - sed?

I'm working on a script to execute a number of items. One being, editing particular files to add certain lines. I'm attempting to utilize sed, but, having issues when running from a bash script. Assistance is greatly appreciated.

My example:

sed -i '14 i\
# add these lines
add these lines to the file
add these line to the file'
/my/file/location | tee /my/file/location

This is suppose to insert the lines after "sed -i '14 i\," into line 14, overwrite and save.

Why the tee?

I'm open to any suggestion. Doesn't need to be sed.

---------- Post updated at 03:43 PM ---------- Previous update was at 02:06 PM ----------

Resolved using awk and writing the output, to a temp file.

Could you please show us how you used awk to make this work so other people reading your thread can learn from your experience?

Try escaping the <new line> characters in the insertion text:

sed -i '14 i\
# add these lines\
add these lines to the file\
add these line to the file
' /my/file/location
a="/file/location"
b="/file/locationb"
awk -v n=14 -v s="my text
my text
my text" 'NR == n {print s} {print}' $a > $b && mv $b $a
1 Like