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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert a new line after every group...

Status
Not open for further replies.

TechieTik

Programmer
Sep 27, 2006
5
US
I have a text (space or pipe delimited) file that is sorted. I want insert a blank new line after each of group.
How do i get this?
Example:
a10|F0093903|87903830
a10|F0093904|87903820
a15|F0083913|87903840
a15|F0093933|87903240
a15|F0093976|87903820
a20|F0093903|87903840
a20|F0093903|87904820
a20|F0093903|87910830
a30|F0093903|87910830

I want to insert a new line after rows 2, 5, 8. The grouping here is based on first column.

Thanks in advance for help.


 
One way:
awk -F'|' 'NR>1 && $1!=a{printf "\n"}{a=$1;print}' /path/to/text

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
It worked. Thanks for the quick response. BTW, I'm new to awk. Can you please explain the script in brief.
 
-F'|' sets the field delimiter to the pipe symbol

'NR>1 && $1!=a{printf "\n"}{a=$1;print}' is the awk code

NR>1 && $1!=a{printf "\n"} means: if (this is not record number 1 NR>1 and && the first field is different from the value in variable a $1!=a) then print a blank line

{a=$1;print} means: store first field in variable a a=$1, then ; print the record print.

see also: man awk


HTH,

p5wizard
 
Thanks p5wizard for the explanation. Thanks again for the timely help PHV.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top