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

Convert short lines to 3-column wide lines. 1

Status
Not open for further replies.

motoslide

MIS
Oct 30, 2002
764
US
This should be an easy one for you awk/printf experts.
I have a text file with hundreds of short lines (all less than 25 characters). I'd like to produce a file consisting of 3 columns, primarily just to make this better to fax to a Client.

Here's what I have:
Nov 28 19:45 bob
Nov 28 21:18 fred
Oct 3 2003 kevin
Aug 7 1998 skippy
Aug 7 1998 bruce
(etc)

Here's what I'd like:
Nov 28 19:45 bob Nov 28 21:18 fred Oct 3 2003 kevin
Aug 7 1998 skippy Aug 7 1998 bruce

I know how to join the lines together, but I don't know how to tell it do join the lines in groups of 3.

Thanks in advance.
 
man pr
pr -a -3 -t </path/to/inputfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 

An explanation of the Awk program.

To get the effect we want, we manipulate the value of [tt]ORS[/tt],
the output-record separator that is appended to every line written
by [tt]print[/tt].

Code:
ORS = NR%3 ? "\t" : RS

When the number of the line just read ([tt]NR[/tt]) isn't divisible by 3,
we want [tt]ORS[/tt] to be a tab; when it
is divisible, we want the ORS to be a newline, which is the default value
of [tt]RS[/tt], the input-record separator.


The [tt]? ... : ... [/tt] syntax is also used in the C language.
Here's what the code above does:
[tt]
if ( NR % 3 ) ORS = "\t" else ORS = RS
[/tt]


If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. For an introduction to Awk, see faq271-5564.

 
Thanks much, Guys. In my case, using the "pr" command was sweet and simple. I'll keep the "awk" example from Futurelet in mind as other needs arise.

Life is good.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top