Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert newline after certain number of fields in long line 2

Status
Not open for further replies.

arunrr

Programmer
Oct 2, 2009
103
US
Hello,

I have the following input file, one continuous line with no newline characters...

TMD,5,5,0,283,144,56.60,279,101.43,1,1,0,38,1,AJS,4,4,0,280,158,70.00,268,104.47,1,1,1,29,2,ABV,3,3,1,266,134,133.00,247,107.69,2,0,0,21,6,WUT,5,5,1,258,133,64.50,292,88.35,1,1,0,36,0,VSH,4,4,0,254,175,63.50,195,130.25,1,0,0,26,7,KCS,5,5,3,252,92,126.00,281,89.67,0,2,0,20,2,IJL,4,4,0,222,92,55.50,270,82.22,0,3,0,17,0,SRT,4,4,0,213,120,53.25,222,95.94,1,0,0,24,5,MJG,4,4,2,192,86*,96.00,251,76.49,0,2,0,20,4,

After every 14 fields (FS=,) i would like to replace ',' with a newline (\n). End result should be...

TMD,5,5,0,283,144,56.60,279,101.43,1,1,0,38,1
AJS,4,4,0,280,158,70.00,268,104.47,1,1,1,29,2
ABV,3,3,1,266,134,133.00,247,107.69,2,0,0,21,6
WUT,5,5,1,258,133,64.50,292,88.35,1,1,0,36,0
VSH,4,4,0,254,175,63.50,195,130.25,1,0,0,26,7
KCS,5,5,3,252,92,126.00,281,89.67,0,2,0,20,2
IJL,4,4,0,222,92,55.50,270,82.22,0,3,0,17,0
SRT,4,4,0,213,120,53.25,222,95.94,1,0,0,24,5
MJG,4,4,2,192,86*,96.00,251,76.49,0,2,0,20,4

Your help is appreciated...
Thanks
Arun
 
This simple command will put a newline before your first line, and keep the , at the end of line:
Code:
 sed 's/\([A-Z]\+,\)/\n\1/g' FILE
It searches for Alpha-Chars, and inserts a newline before.

This is the not so simple command:
Code:
sed  's/\(\([A-Z0-9.*]\+,\)\{13\}\)\([A-Z0-9]\+\),/\1\3\n/g' FILE
It searches for 13 elements followed by a comma, a next one with comma, and inserts the first bunch and the second, but the second without comma, instead with a newline.

Do you know sed in general?

don't visit my homepage:
 
What about this ?
Code:
awk 'BEGIN{RS=","}!(NR%14){print x $0;x="";next}{x=x $0","}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for the valuable reply. They all work great.
AR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top