Hi Gurus,
I've a sample output from a script with a header as shown below.
The formatting is a little bit out of alignment when it's sent out via email.
Sample output:
Label Date New Data #AB Removed #CD Net Change
Statistic 2012-06-03 21807 mb 206 -46503 mb 1 -24695 mb
Statistic 2012-06-04 28 mb 1 0 mb 28 mb
Statistic 2012-06-03 13926 mb 222 0 mb 13926 mb
Statistic 2012-06-03 166624 mb 704 0 mb 166624 mb
Desired output:
Label Date New Data #AB Removed #CD Net Change
Statistic 2012-06-03 21807 mb 206 -46503 mb 1 -24695 mb
Statistic 2012-06-04 28 mb 1 0 mb 28 mb
Statistic 2012-06-03 13926 mb 222 0 mb 13926 mb
Statistic 2012-06-03 166624 mb 704 0 mb 166624 mb
Also, for column: "#AB " and "#CD ", sometimes there is a value, sometimes it's just empty so need to take this into consideration for output alignment.
How to make it in a better format with proper alignment?
Any help will be much appreciate.
Thank you.
---------- Post updated at 06:35 PM ---------- Previous update was at 09:11 AM ----------
Hi Gurus,
Anybody can help on this formatting?
Thanks in advance.
agama
June 5, 2012, 8:29pm
2
Nothing but brute force here:
awk '
BEGIN { fmt = "%-10s %-10s %-10s %-10s %-10s %-10s %-10s\n"; }
/Label/ { printf( fmt, $1, $2, $3 " " $4, $5, $6, $7, $8 " " $9 ); next; }
NF == 10 { printf( fmt, $1, $2, $3 " " $4, $5, $6 " " $7, $8, $9 " " $10 ); next; }
NF == 9 && $6 == "mb" {
printf( fmt, $1, $2, $3 " " $4, " ", $5 " " $6, $7, $8 " " $9 )
next;
}
NF == 9 {
printf( fmt, $1, $2, $3 " " $4, $5, $6 " " $7, " ", $8 " " $9 )
next;
}
NF == 8 {
printf( fmt, $1, $2, $3 " " $4, " ", $5 " " $6, " ", $7 " " $8 )
next;
}
' input-file >output-file
1 Like
superhonda123:
The formatting is a little bit out of alignment when it's sent out via email.
Also, for column: "#AB " and "#CD ", sometimes there is a value, sometimes it's just empty so need to take this into consideration for output alignment.
How to make it in a better format with proper alignment?
It would have helped to post the script. Changing scripts we don't know is a bit hard to do and my crystal ball which reveals any secret to me is in repair at the moment. Therefore just some general directions instead of concrete help:
Have a look at the "printf()" function. It works the same as system call (in C/C++), as ksh-builtin or as awk-builtin. You provide a format string and a list of variables to fill the spots. In the format strings you can define the output width you want to use and if you want it left- or right-aligned. By using proper values you get tabular column-output automatically.
I hope this helps.
bakunin
Hi All,
Thanks for your response and help.
I've tried the method given by Agama, but the output is still does not give the proper alignment. Sorry.
I guess it might be a better idea to use tab instead.
Also, I was thinking if it's a good idea to insert something like "NA" if column "#AB " and "#CD " is blank (empty). This might helps in the formatting.
Anybody else can help?
Thanks.