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

HELP WITH AWK 2

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I have the following awk script used to change the output to display 4 columns from an imput file with 1 column:

BEGIN {
if (cols == "")
cols=4;
}

{ printf("%-20s ", $1, $2, $3, $4"\n") }
!(FNR % cols) {print "\n"}
The output displays like the example below:

c00001 c00015 c00017 c00049
c01234 c01231 c01321 c00123


Can someone please explain to me what I am doing wrong?

Thanks,

John
 
BEGIN {
if (cols == "")
cols=4;
}

{ printf("%-20s ", $0) }
!(FNR % cols) {print "\n"}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Try this:
Code:
BEGIN{if(cols==&quot;&quot;)cols=4}
{printf &quot;%-20s&quot;,$0;if(!(NR%cols))printf &quot;\n&quot;}
END{if(!(NR%cols))printf &quot;\n&quot;}
You can also consider pr, like this:
Code:
pr -t -a -4 -w 80 YourInputFile

Hope This Help
PH.
 
Vlad,

I have already tried that and it returns the same results. Could it be something to do with my display or terminal settings? I would like the columns to appear like the example below:

c00001 c00015 c00017 c00049
c01234 c01231 c01321 c00123
c01214 c01211 c01321 c00452

Thanks,

John
 
PHV,

Thanks, that works great!! Do you have any idea why Vlad's example was not working? I am almost certain that I have used that before and had successful results.

Thanks,

John
 
john,
that's what I'm getting. Spool your output to a file and take a look at it in the editor.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad,

Even when I redirect my output to a file and view it with an editor it is appearing diagonally. I'm not sure what the problem is.

Thanks,

John
 
As a footnote, it is probably worth mentioning the paste command. To display 4 columns from an input file with 1 column....

paste - - - - <file1

which is tab separated, but if you prefer fixed-width ....

paste - - - - <file1 |awk '{printf &quot;%-20s%-20s%-20s%-20s\n&quot;,$1,$2,$3,$4}'


 
Thanks, Ygor!!

I wouldn't have thought of using the paste command.

Thanks,

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top