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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert a new line before every 5th line in a file
# 1  
Old 05-25-2012
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
Code:
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 every 5th line.
# 2  
Old 05-25-2012
Hi

Code:
awk '!(NR%5) {print "QUERY"} {print $0}' file

Guru.
# 3  
Old 05-25-2012
Quote:
Originally Posted by guruprasadpr
Hi

Code:
awk '!(NR%5) {print "QUERY"} {print $0}' file

Guru.
Wow, that works fine Smilie !! How to make the sed to work?
# 4  
Old 05-25-2012
Hi


Code:
sed 'n;n;n;n;s/.*/QUERY\n&/;' file

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 05-25-2012
Some sed do not know \n in the replacement part:

Code:
sed 'n;n;n;n;s/^/QUERY\
/' infile

Code:
sed 'n;n;n;n;i\
query\
' infile



--
GNU sed:
Code:
sed '0~5s/^/QUERY\n/' infile


Last edited by Scrutinizer; 05-25-2012 at 03:55 AM..
This User Gave Thanks to Scrutinizer For This Post:
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. 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

3. 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

4. Shell Programming and Scripting

Insert a line in a file by deleting that line from another file

Hi all, I have a huge file(size more that 5GB). I want to do some manupulation with the records and write to another file. As the size of the file is huge and there is a space constraint in that directory, I want to delete that record from first file after writing it in to second file.... (3 Replies)
Discussion started by: gani_85
3 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. Programming

insert line into a file

how to insert a line of text that is next to the current line(file pointer pointing to) in the file ?? :wall: ex: suppose a file named 'Sample' has the following content in it. this is to give clear idea about the problem if file pointer is pointing to the first line then i want to... (3 Replies)
Discussion started by: kavitha rao
3 Replies

7. Shell Programming and Scripting

Adding a blank line after every 5th line

Hello... I have a file which contain certain number of records. I want to generate another file from this file which will contain 1st line as a blank line & after every 5 lines one blank line will be inserted. How to achieve this through shell scripting? Thanks... (5 Replies)
Discussion started by: 46019
5 Replies

8. UNIX for Advanced & Expert Users

help with insert line into file

hi..i wanted to know is there any way to take input from the user and copy that a particular line number in a particular file for eg . i wanted to create acl in squid at line number 2400 in squid.conf file so the user gives an input like acl inetblock src 192.168.0.0/255.255.0.0 so is there... (4 Replies)
Discussion started by: tarunicon
4 Replies

9. Shell Programming and Scripting

insert a line in a file

Hello guys, Need to know how to insert a line at top of the file, without using temp files. Can we do it on the fly? Regards, Rishi (7 Replies)
Discussion started by: RishiPahuja
7 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