The simplest method of the AWK output N columns

I recently encountered a scene and need to output the first N columns of a text message.

It is well known that cut can specify the separator and specify the range of the column. For example, cut -d' '-f-4 is output with a space as a separator The first 4 columns. But the separator of cut can only be one character, which is far less useful than awk.

After a simple search of various information on the Internet, there is no simple method for awk to output the first N columns. The most commonly seen is to use a for loop to output [1][2]:

$ awk'{ for(i=1; i<=2; i++) {print $i} }'

Here is a method to modify the NF mark output:

$ awk'{NF=4}1'

This is the output of the first 4 columns, for example:

$ dpkg- l | grep ^ii | head -n 3 | awk'{NF=4}1'ii accountsservice 0.6.45-1ubuntu1 amd64ii ​​acl 2.2.52-3build1 amd64ii ​​acpid 1:2.0.28-1ubuntu1 amd64

References

  1. How to print first'n' columns?
  2. Use awk to print the last N columns of a file or a pipe< /li>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 517 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.