Skip to main content
added 166 characters in body
Source Link

Try the following:

Your original file

$ cat <<'EOF' > filenamefile.txt old_line101 line old_line201 line old_line302 line EOF02 line EOF 

insert into line 3 using a heredoc.

$ cat <<'EOF' | sed -i "2r"3r /dev/stdin" filenamefile.txt new_line1 new_line1text to insert EOF   

you get this result

$ cat filenamefile.txt old_line101 line old_line201 line new_line1text to insert new_line102 line old_line302 line 

UPDATED: Following the comment of @Kusalananda

$ cat <<'EOF' > filename.txt old_line1 old_line2 old_line3 EOF $ cat <<'EOF' | sed -i "2r /dev/stdin" filename.txt new_line1 new_line1 EOF   $ cat filename.txt old_line1 old_line2 new_line1 new_line1 old_line3 

Try the following:

Your original file

$ cat <<'EOF' > file.txt 01 line 01 line 02 line 02 line EOF 

insert into line 3 using a heredoc.

$ cat <<'EOF' | sed -i "3r /dev/stdin" file.txt text to insert EOF 

you get this result

$ cat file.txt 01 line 01 line text to insert 02 line 02 line 

UPDATED: Following the comment of @Kusalananda

Source Link

$ cat <<'EOF' > filename.txt old_line1 old_line2 old_line3 EOF $ cat <<'EOF' | sed -i "2r /dev/stdin" filename.txt new_line1 new_line1 EOF $ cat filename.txt old_line1 old_line2 new_line1 new_line1 old_line3