打印字段“N”到行尾
我想就 awk 中遇到的问题获得帮助或指导。
我有一个包含超过 5 个字段的制表符分隔文件。我想输出不包括前 5 个字段的字段。
您能告诉我如何编写 awk 脚本来完成此任务吗?
最好的, jianfeng.mao
请注意以下善意的评论:
我的文件中有很多字段。不同的行有不同数量的字段。每行的字段数不是标准的。
I would like to have help or direction on a problem I have in awk.
I have a tab-delimited file with more than 5 fields. I want to output the fields excluding the first 5 fields.
Could you please tell how to write an awk script to accomplish this task?
Best,
jianfeng.mao
Do Note the following kind comment:
There are many fields in my files. Different lines have a different number of fields. The number of fields per line is not standard.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我的制表符分隔文件
temp.txt
中,如下所示根据您的更新,我强烈建议使用
cut
:将 field6 打印到行尾。
注意
-d
指定分隔符,但制表符是默认分隔符。您可以在
awk
中执行此操作,但我发现cut
更简单。使用
awk
,它看起来像这样:如果我的制表符分隔文件 temp.txt 如下所示
将仅打印第 6 个字段。如果分隔符是制表符,则无需设置 -F 即可工作,但我喜欢尽可能设置字段分隔符。
同样地,也会削减。
我有预感你的问题比这个更复杂,所以如果你回复我的评论,我可以尝试扩展我的答案。
In my tab delimited file
temp.txt
it looks like the followingAs per your update, I strongly recommend using
cut
:will print field6 to end of line.
Note
-d
specifies the delimiter, but tab is the default delimiter.You can do this in
awk
, but I findcut
to be simpler.With
awk
it would look like this:if my tab delimited file temp.txt looks like the following
will print only the 6th field. if the delimiter is tab it will likely work without setting -F, but I like to set my field-separator when I can.
similarly so too would cut.
I have a hunch your question is a bit more complicated then this, so if you respond to my comment I can try and expand on my answer.
我同意 matchew 使用
cut
的建议:它是完成这项工作的正确工具。但是,如果这只是成为更大的awk
脚本的一部分,请按以下步骤操作:I agree with matchew's suggestion to use
cut
: it's the right tool for this job. But if this is just going to become a part of a largerawk
script, here's how to do it:perl方式?
所以,
会产生
perl way?
so,
will produce
我使用
-vFS='\t'
而不是-F'\t'
因为 awk 的某些实现(例如 BusyBox 的)不支持后一种构造中的 C 转义。I use
-vFS='\t'
rather than-F'\t'
because some implementations of awk (e.g. BusyBox's) don't honor C escapes in the latter construction.