Visit Our UNIX and Linux User Community


New line after every 3 lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting New line after every 3 lines
# 1  
Old 07-21-2009
New line after every 3 lines

Hi all,

My file is of below format:
Code:
abc,111,222,333
xyz,398,399,834
jdj,398,947,487
nmn,535,33,34
sss,43,3,53
ks,343,3,4
d,94,94999

I want to insert 2 rows(NULL,0,0,0) after every 3 lines. shown as below:

o/p:
Code:
abc,111,222,333
xyz,398,399,834
jdj,398,947,487
NULL,0,0,0
NULL,0,0,0
nmn,535,33,34
sss,43,3,53
ks,343,3,4
NULL,0,0,0
NULL,0,0,0
d,94,94999

could anyone help me ?Smilie

Use CODE-tags when displaying code, data or logs to enhance readability and to preserve formatting like indention etc., ty.

Last edited by zaxxon; 07-21-2009 at 06:57 AM.. Reason: code tags
# 2  
Old 07-21-2009
Code:
$> awk 'NR % 3 == 0 {print; print "NULL,0,0,0\nNULL,0,0,0"; next} {print}' infile
abc,111,222,333
xyz,398,399,834
jdj,398,947,487
NULL,0,0,0
NULL,0,0,0
nmn,535,33,34
sss,43,3,53
ks,343,3,4
NULL,0,0,0
NULL,0,0,0
d,94,94999

# 3  
Old 07-21-2009
Question

Thanks zaxxon.

A slight change,

My file is of below format:

abc,111,222,333,jjj,22
xyz,398,399,834,ww,33
jdj,398,947,487,eee,44
abc,535,33,34,jjj,22
sss,43,3,53,dd,ter,33
ks,343,3,4,jsj,33
d,94,94999,re,ere
cc,33,53453,xd,22
abx,343,33,jjj,55

As we can see in 5th column, "jjj", the number of rows between two jjj shoud be 7.

the o/p should be as :
abc,111,222,333,jjj,22
xyz,398,399,834,ww,33
jdj,398,947,487,eee,44
NULL,0,0,0,0,0
NULL,0,0,0,0,0
NULL,0,0,0,0,0
NULL,0,0,0,0,0
abc,535,33,34,jjj,22
sss,43,3,53,dd,ter,33
ks,343,3,4,jsj,33
d,94,94999,re,ere
cc,33,53453,xd,22
NULL,0,0,0,0,0
NULL,0,0,0,0,0
abx,343,33,jjj,55

could anyone help me ?

Previous Thread | Next Thread
Test Your Knowledge in Computers #629
Difficulty: Easy
In PHP, False == 0;
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

3. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Echo printing a line in 2 lines; expected to print in one line

Dear All, fileName: therm.txt nc3h7o2h 7/27/98 thermc 3h 8o 2 0g 300.000 5000.000 1390.000 41 1.47017550e+01 1.71731699e-02-5.91205329e-06 9.21842570e-10-5.36438880e-14 2 -2.99988556e+04-4.93387892e+01 2.34710908e+00 4.34517484e-02-2.65357553e-05 3 ... (7 Replies)
Discussion started by: linuxUser_
7 Replies

5. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

6. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

7. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

8. UNIX for Dummies Questions & Answers

append following lines to 1st line, every 3 lines

I have output like this: USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 ... where USER_ID is a unique user login followed by their login timestamp and... (6 Replies)
Discussion started by: MaindotC
6 Replies

9. Shell Programming and Scripting

Break lines up into single lines after each space in every line

It sounds a bit confusing but what I have is a text file like the example below (without the Line1, Line2, Line3 etc. of course) and I want to move every group of characters into a new line after each space. Example of text file; line1 .digg-widget-theme2 ul { background: rgb(0, 0, 0) none... (7 Replies)
Discussion started by: lewk
7 Replies

10. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies

Featured Tech Videos