本文最后更新于2014年11月14日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!


Print all but the first three columns
Too cumbersome:

A solution that does not add extra leading or trailing whitespace:


Another approach using the ternary operator is Sudo_O’s solution:

And EdMorton gives a solution that preserves the original whitespace between fields:

The solution given by larsr in the comments is almost correct:

This is the fixed and parametrized version of larsr solution:

All other answers are nice but add extra spaces:

Example of answer adding extra leading spaces:

Example of answer adding extra trailing space


use cut

or if you insist on awk and $13 is the last field


The correct way to do this is with an RE interval because it lets you simply state how many fields to skip, and retains inter-field spacing for the remaining fields.

e.g. to skip the first 3 fields without affecting spacing between remaining fields given the format of input we seem to be discussing in this question is simply:

If you want to accommodate leading spaces and non-blank spaces, but again with the default FS, then it’s:

If you have an FS that’s an RE you can’t negate in a character set, you can convert it to a single char first (RS is ideal if it’s a single char since an RS CANNOT appear within a field, otherwise consider SUBSEP), then apply the RE interval subsitution, then convert to the OFS. e.g. if chains of “.”s separated the fields:

Obviously if OFS is a single char AND it can’t appear in the input fields you can reduce that to:

Then you have the same issue as with all the loop-based solutions that reassign the fields – the FSs are converted to OFSs. If that’s an issue, you need to look into GNU awks’ patsplit() function.

Pretty much all the answers currently add either leading spaces, trailing spaces or some other separator issue. To select from the fourth field where the separator is whitespace and the output separator is a single space using awk would be:

To parametrize the starting field you could do:

And also the ending field:



Print all but the first three columns



This will remove all spaces …(删除所有空格)

This will remove trailing spaces…(删除尾部空格)

This will remove leading spaces…(删除前导空格)

This will remove both trailing and leading spaces(同时删除前后空格)




设置一个数组,但是用不同的数组下标进行区分(比如这里的:all 和 $2 ,只是下标不同,但用的同一个数组),最后用个END进行打印即可;当然也可以设置多个数组,但如何关联起来就是个技巧问题了(跟具体需求相关)。

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!


  1. awk的FS和OFS的使用学习 – Simple awk command issue (FS, OFS related)

    $ head rivalHost.txt | awk -F'\t' OFS='\t' 'NR>1 && $(11)=="200" {print $1,$6,$7,$(11)}'
    awk: syntax error at source line 1
    context is
    >>> OFS=\ <<< t awk: bailing out at source line 1 #如果想让OFS变量生效,需要将 OFS='\t' 这个放在最后,否则会有上面的报错(放在BEGIN块中试了也不行)


电子邮件地址不会被公开。 必填项已用*标注