1834127 Members
2210 Online
110064 Solutions
New Discussion

awk

 
Jacques Carriere
Regular Advisor

awk

awk '{print $2" "$3" "$9" "$10" "$11" "$12" "$13" "$14}' > $tf1

Q: what is the correct syntax to copy all fields to create lines longer than 72 characters.

Jacques
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: awk

Awk can easily print lines longer than 72 characters so your question is unclear.

In general, if you wish to format the output, printf rather than simply print is preferred.

awk '{ printf ("%-10.10s %5s %-12.12s\n",$2,$3,$9") }'

would print the 1st 3 fields in your example above followed by a LF. Awk's printf is modeled after the C printf function so man 3 printf for details although if you will try the above example, it should be fairly obvious what the above example does.
If it ain't broke, I can fix that.
Basheer_2
Trusted Contributor

Re: awk

Hi Jacques

awk '{if (length>72) {print length, $0} }' file-name
Jacques Carriere
Regular Advisor

Re: awk

if I reduce my display screen to execute my script, awk does not copy all fields. Can someone give me the correct syntax to force awk to copy all fields no matter the size of the display screen.

A. Clay Stephenson
Acclaimed Contributor

Re: awk

Note that in your example, you are redirecting stdout to a file and thus the screen size is completely irrelevant. I suspect that the actual problem is that your terminal is set to no eol wrap and anything beyond the width of the terminal is being truncated for display purposes although the data exist in the file. The actual limit on awk line length (record size) is 3000 characters and you are nowhere near that. If you use the Gnu version of awk even that 3000 character limit goes away.
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: awk

Along was what Clay says, if you want to see what's in your file you can use several dumping commands:
$ vis -n file
$ xd -tc file
Jacques Carriere
Regular Advisor

Re: awk

If I reduce my display screen, I only get a few fields copied. I expand the display screen, I get everything copied correctly eventhough awk is executed from inside a script. What can I do to tell awk to extract all my fields?
A. Clay Stephenson
Acclaimed Contributor

Re: awk

I simply don't know how to explain this any better than to say that the data are there but are not all being displayed on your terminal (or terminal emulator). If you can enable eolwrap then you will probably see the data.

Try running the attached script. It will produce a line much longer than 72 chars and then send it to awk. It will then attempt to print it field by field. Finally the input file is comapred to the output file. I expect a null output from diff.

If it ain't broke, I can fix that.