Thursday, January 31, 2019

Adding line in middle of file using command in linux

No comments
Hi Dear Friends,

In this post We would like to share infinity knowladge My Good experience and Best solution For .

Laravel Examples

Hi Guys,
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.
Try this it will help you and for more information click here: Laravel Examples.

Read :

I hope you like this Post, Please feel free to comment below, your Any Idea, suggestion and problems if you face - I am here to Resolve your Any problems.
We hope it can help you...

No comments :

Post a Comment