Insert Multiple New Line in a File Using Shell Script?
Do you want to insert multiple lines below of a specific line sed command will help us with different cases,
For example few lines saved in a file name of linuxfaq.txt that contain
colors
scripts
dash
ftpsh
scripts
dash
ftpsh
but i would like to add three lines one by one "red, green, blue" next line of colors
Note : -i specified that save the changes in file
sed -i "/colors/ared\ngreen\nblue" linuxfaq.txt
Once you have executed that command output will be
# cat linuxfaq.txt
colors
red
green
blue
scripts
dash
ftpsh
If are you Passing String by Variable,
color="blue"
sed -i "/colors/ared\ngreen\n$color" linuxfaq.txt
How you can insert lines from a file.
#vi toadd.txt
red
green
blue
red
green
blue
sed '/colors/r toadd.txt' linuxfaq.txt
Do you want to append a string at the end of specific line?
For example,
# cat linuxfaq.txt
Hierarchical File System mac
ext4
FAT32 NTFS windows
Hierarchical File System mac
ext4
FAT32 NTFS windows
Need to add a "linux" word at end of "ext4"
sed 's/ext4/& linux/' linuxfaq.txt
# cat linuxfaq.txt
Hierarchical File System mac
ext4 linux
FAT32 NTFS windows
Comments (0)