AWK学习_4

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

如何用awk实现打印指定列的功能?

Print all but the first three columns
Too cumbersome:


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

Demo:

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

else

====
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

==

如何删除输出中的前/后导空格(类似于trim的作用)?

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

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

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

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

搜索关键字:http://search.aol.com/aol/search?q=linux+how+to+trim+whitespace+of+output

==

用awk去重(获取两种类型的去重数据的方法)

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

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

《AWK学习_4》上有1条评论

  1. awk的FS和OFS的使用学习 – Simple awk command issue (FS, OFS related)
    https://stackoverflow.com/questions/16203336/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块中试了也不行)

发表评论

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