Operating System - HP-UX
1836779 Members
2461 Online
110109 Solutions
New Discussion

How to display a output by column clearly

 
SOLVED
Go to solution
haeman
Frequent Advisor

How to display a output by column clearly

I use the below command can show the output correctly .
cat myfile | awk -F: '{ print $1,$2}'

the output is as below

peter 5424
amy 4324
george 4324
stephanie 6583

but each column only have 1 space only , so it is not good to read, if I want the output show by column more clearly as below ,

peter 5424
amy 4324
george 4324
stephanie 6583


can advise what can i do ? thx
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: How to display a output by column clearly

Hi:

Use 'printf' for formatted printing. Also, eliminate the useless 'cat' process! The arguments to 'awk' specify the input file:

# awk '{printf "%-15s %6d\n",$1,$2}' myfile

peter 5424
amy 4324
george 4324
stephanie 6583

Regards!

...JRF...
Rasheed Tamton
Honored Contributor
Solution

Re: How to display a output by column clearly

You can also use by adding tab (\t) keys in between the columns as below:

awk '{print $1, "\t\t", $2}' myfile


Rgds.
Dennis Handly
Acclaimed Contributor

Re: How to display a output by column clearly

>Rasheed: You can also use by adding tab

Tabs don't work well if you have fields that vary in width from "peter" to "stephanie".
Tabs only work if you have a human adding them or advanced AI technology to do the computations. And then you might as well use JRF's printf fixed width.
Rasheed Tamton
Honored Contributor

Re: How to display a output by column clearly

If that is the case, then this would be the solution.


awk '{ printf("%-15s\t%10d\n", $1, $2)} ' filename

and with two tabs:

awk '{ printf("%-15s\t\t%10d\n", $1, $2)} ' filename

Rgds,
Rasheed Tamton.
James R. Ferguson
Acclaimed Contributor

Re: How to display a output by column clearly

Hi (agan):

> Rasheed: If that is the case, then this would be the solution.
> awk '{ printf("%-15s\t%10d\n", $1, $2)} ' filename

...and what value does that add? Why would THIS be the solution?

...JRF...
Rasheed Tamton
Honored Contributor

Re: How to display a output by column clearly

Oops!

James (JRF) I agree. I realized it after I posted. I have not checked your first posting correctly. I had looked the output of the command from your posting and did not check your command itself. I did it after I posted it. It is 99.9% same. Mine just gives additional tabs!!!

Regards.
James R. Ferguson
Acclaimed Contributor

Re: How to display a output by column clearly

Hi (again):

> Rasheed.

Thanks. I find that embedded tab characters are more trouble than not. Most generally a 'tab' is considered 8-spaces worth but that is only a default.

Printing lines with tabs embedded between fields can result in mis-aligned output in some printer configurations. To correct this, when tabs have been used, use the 'expand' filter described in the manpages.

If you want extra whitespace in formatted printing, you can always do things like:

# printf "%12s%8s%12s\n" "thank" "" "you"

...that's three (3) fields with the second being the imposition of eight (8) blanks --- a "tab-worth" by default.

Regards!

...JRF...

Rasheed Tamton
Honored Contributor

Re: How to display a output by column clearly

Thank you James. It makes sense.

So using your method (without using tabs), it would be like this:

awk '{printf "%-15s%10s%6d\n",$1,"" ,$2}' filename

Regards,
Rasheed Tamton.
James R. Ferguson
Acclaimed Contributor

Re: How to display a output by column clearly

Hi:

> Rasheed: So using your method (without using tabs), it would be like this:

> awk '{printf "%-15s%10s%6d\n",$1,"",$2}' filename

Yes, that would be the way I would write it to impose ten (10) blanks between the two data fields. I find that this is much easier to read than [which this forum will render less readable, too!]:

# awk '{printf "%-15s %6d\n",$1,$2}' filename

Regards to you Rasheed!

...JRF...