Insert line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Insert line
# 1  
Old 03-24-2014
Insert line

Hello

Is there any sed command to insert a line after nth line... have a file of 500000 lines

Thanks
# 2  
Old 03-24-2014
Hello,

Here is an awk example for same, it may help.
For example an input file is:

Code:
1  5
2  4
3  3

Code is:
Code:
awk 'NR==2 {print "checking the insertion."} 1' file_name

Output will be as follows.

Code:
1  5
checking the insertion.
2  4
3  3


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 03-24-2014
Insert line after every 7th line:
Code:
sed -n 'p;n;p;n;p;n;p;n;p;n;p;n;p;i\inserted line' file_1 >file_2

This User Gave Thanks to spacebar For This Post:
# 4  
Old 03-24-2014
Try sed with i command to insert a line before match for your request to add only one line.

Code:
$ cat file.txt
1
2
3
4
5

Code:
$  sed "4 i\Inserted new line" file.txt
1
2
3
Inserted new line
4
5

This User Gave Thanks to mjf For This Post:
# 5  
Old 03-28-2014
Thank you all for the replies

I ended up writing a small fortran code to do the insertion I was looking for by using
Code:
m = 90000
n = 288
do i = 1, m
 do j = 1, n
read() original file
write() to the new file
end do
insert line
enddo


Last edited by Scrutinizer; 03-28-2014 at 03:57 PM.. Reason: icode tags changed to code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

2. UNIX for Beginners Questions & Answers

Insert line

My file looks like this. When I see dn:, i'd like to insert a line above that. Sorry, i have no idea how to insert a line above so i won't be able to put in a code tage. Thank you so much for your time and assistance on this request. dn: OU=Groups,O=lwsnrmdata,O=lwsn,DC=cinemark,DC=com... (5 Replies)
Discussion started by: loktamann
5 Replies

3. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

4. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

5. Shell Programming and Scripting

Sort a line and Insert sorted word(s) in a line

Hello, I am looking to automate a task - which is updating an existing access control instruction of a server and making sure that the attributes defined in the instruction is in sorted order. The instructions will be of a specific syntax. For example lets assume below listed is one of an... (6 Replies)
Discussion started by: sanjayroc
6 Replies

6. Shell Programming and Scripting

Insert a new line before every 5th line in a file

Hi, I need to insert a new line containing the string "QUERY" above every 5 lines. The below piece of code inserts a new line after every 5th line awk '{print $0} !(NR%5) {print "QUERY"}' sed 'n;n;n;n;G;' --> I do not know how to give "QUERY" string here But I need to insert it before... (4 Replies)
Discussion started by: royalibrahim
4 Replies

7. Shell Programming and Scripting

How to insert and delete any line after desire line

like i have file like abc 123 pqr bbbb ttttttttt t tttt ------------------ i want to insert "class" after pqr and t lines please suggest me. (4 Replies)
Discussion started by: RahulJoshi
4 Replies

8. Shell Programming and Scripting

insert a line after specific line

Hii, I have a file like this-- Here i want to replace the line o: Torry Harris with o: Torry arris Business Solutions and in file there are places where this line is not there after the Mobile number,so i want to insert the line as --o: Torry arris Business Solutions. i can replace it... (8 Replies)
Discussion started by: namishtiwari
8 Replies

9. UNIX for Dummies Questions & Answers

how to insert a line number on every line

hi... i have a file with data and would like to insert a number and bracket 1) ...2) at the beginning of every successive line; to add some formatting to the text (3 Replies)
Discussion started by: mopimp
3 Replies

10. UNIX for Advanced & Expert Users

Insert a line as the first line into a very huge file

Hello, I need to insert a line (like a header) as the first line of a very huge file (about 3 ml rows). I am able to do it with sed, but redirecting the output and creating a new file takes quite some time. I was wondering if there was a more efficient way of doing it? Any help would be... (3 Replies)
Discussion started by: shriek
3 Replies
Login or Register to Ask a Question

Featured Tech Videos