Re: awk remove lines format to columns (225 Views)
Reply
Super Advisor
Ratzie
Posts: 855
Registered: ‎01-10-2002
Message 1 of 3 (225 Views)
Accepted Solution

awk remove lines format to columns

I am trying to write a ksh script that pulls data to a file, have it working no problem.
format of file.

13-JAN-09-22:17:00
12-JAN-09-07:13:07
NULL
NULL
13-JAN-09-21:08:04

Some times the NULL values will also be dates.

So I want to take this and join lines and format to columns.

Here is what I have working:
cat test.1| tr '\n' '\t'|awk '{print $1, $2, $3, $4, $5}'>>monitor.log

what I would like to do is have each variable line up at certain column.

Because right now the file looks fine:
13-JAN-09-22:16:38 12-JAN-09-07:12:57 NULL NULL 13-JAN-09-21:08:04
13-JAN-09-22:17:00 12-JAN-09-07:13:07 NULL NULL 13-JAN-09-21:08:04

But if I get an entry in $3 $4, nothing will line up anymore.


So how do I format $1 to start at 1, $2 start at 15 $3 start at the next 15... on so on.


Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 3 (225 Views)

Re: awk remove lines format to columns

Hi:

> So how do I format $1 to start at 1, $2 start at 15 $3 start at the next 15... on so on.

You use formatted print statements:

# cat test.1|tr '\n' '\t'|awk '{printf "%-15s %-15s %-15s %-15s %-15s\n",$1,$2,$3,$4,$5}'

This lef-justifies fields in units of 15-characters. See the manpages for 'printf'.

Regards!

...JRF...
Super Advisor
Ratzie
Posts: 855
Registered: ‎01-10-2002
Message 3 of 3 (225 Views)

Re: awk remove lines format to columns

Works PERFECT!
I have been fiddling with printf and nothing but errors.
Put a question in and I got a reply with 10 minutes!
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.