0

I would like to print data in fixed column. I mean the column should be like in table. I used printf. Input:

To find information use the search box on the top right corner of the screen, or categorically browse the Wiki using the Documentation topic links provided below.

Find something you would like to add or edit? The Getting started section (below) gives contributors a few pointers on how to start editing articles.

Both official documentation as well as community-contributed contents can be found on the Wiki. Official documents (created by the Gentoo Documentation Team) are located in the Gentoo Project name space. 

AWK:

{
for(i=1;i<=5;i++){
printf "%20s",$i
if (i==5){print "\n"}}
}

Result

          To                find         information                 use                 the



        Find           something                 you               would                like



        Both            official       documentation                  as                well

There is a lot of space on the left and below the line with columns. There are other way to show this?

0
awk -v numlines=$( wc -l t | awk '{print $1}' ) '{ if (NF>0) { for (i=1;i<=5;i++) printf("%-20s ",$i) ; if (NR != numlines) { print "" } } }' inputfile

The 'numlines=$(' portion puts the number of lines in the input file into a variable available to awk called numlines. 'NF>0' ensures blank lines are ignored. 'printf(%-20s' ensures the fields are left justified and 20 characters wide. The 'NR != numlines' prints a line feed except at the end of the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.