If you want to add a line in file at specific line number through script. Below examples will help you to do it.
For example you have a file named file1.txt, file content are as below.
root@tecadmin:~ # cat file1.txt tecadmin 1 tecadmin 2 tecadmin 4
Now you required to add text “tecadmin 3” at line number 3, use below command
root@tecadmin:~ # sed '3itecadmin 3' file1.txt > file1.txt.tmp
Above command will create a new file file1.txt.tmp with expected output
root@tecadmin:~ # cat file1.txt.tmp tecadmin 1 tecadmin 2 tecadmin 3 tecadmin 4
Replace origin file with tmp file
root@tecadmin:~ # cp file1.txt.tmp file1.txt
Details of the `sed` command:
sed: is the command itself.
2: line number where new line will be inserted.
i: parameter, which told sed to insert line.
tecadmin 2: text to be added.
file1.txt: is the file in which new line need to add.
file1.txt.tmp: New file with updated content.