Use awk to format and display tables

Source: Internet
Author: User
Tags print format

Link: http://blog.csdn.net/kongxx/article/details/8284952

I encountered a problem in my work today. I need to filter out some command line outputs and output the results in a table-like format.

In this case, we assume that the output of a command is as follows. In this case, we filter some of the rows that fall into the test.txt file.

ID   Name    Designation    Department  Salary100  Thomas  Manager        Sales       $5,000200  Jason   Developer      Technology  $5,500300  Sanjay  Sysadmin       Technology  $7,000400  Nisha   Manager        Marketing   $9,500500  Randy   DBA            Technology  $6,000

The following is a simple awk file, which is saved to the test. awk file.

function format(value, maxlen, align) {len=length(value);lendiff=maxlen-len;if (lendiff>0) {for (k=0; k<lendiff; k++) {if(align=="right") {value=value" ";} else {value=" "value;}}}return value;}BEGIN {rows=0;cols=5;}{if(NF==cols) {for(i=1; i<=NF; i++) {if (length($i) > lengths[i]) lengths[i]=length($i);}rows++;for(col=1; col<=NF; col++) {data[rows, col]=$col;}}}END {for(i=1; i<=length(lengths); i++) {lengths[i]=lengths[i]+4;}for(i=1; i<=rows; i++) {print format(data[i, 1], lengths[1]), format(data[i, 2], lengths[2], "right"), format(data[i, 3], lengths[3], "right"), format(data[i, 4], lengths[4], "right"), format(data[i, 5], lengths[5])}}

The first is a format function used to format some strings. This function contains three parameters: the first parameter value is the string to be formatted, the second parameter is the length of the string to be formatted, and the third parameter is used to indicate that when the length of the string is smaller than maxlen, spaces are left before or after the string.

Next, the begin section defines two variables. Rows is used to indicate the total number of lines in the text content that need to be processed. The NR variable is not used because of the Null Line problem; cols is used to define the number of columns to be displayed. It is assumed that all columns are required, so it is defined as 5.

In the action section, the rows with column 5 are processed. You can write your own filter conditions as needed. In the example above, too many filters are not performed for the purpose. This section uses two loops: the first loop is used to calculate the maximum length of each column in the row and store it in the lengths array. The second loop is to put the required data into a two-dimensional array;

The last part is the end part. This part also contains two loops: The first loop is to add the maximum length of each column to 4, in order to add some intervals between each column; the second loop is used to format the output. The name, designation, and department columns are left aligned, and the number columns are right aligned.

Test. Run the awk-F test. awk test.txt command on the command line. The following result is displayed:

     ID Name       Designation     Department         Salary    100 Thomas     Manager         Sales              $5,000    200 Jason      Developer       Technology         $5,500    300 Sanjay     Sysadmin        Technology         $7,000    400 Nisha      Manager         Marketing          $9,500    500 Randy      DBA             Technology         $6,000

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.