用awk后打印第一列第二列,一列,每四个 [英] Use awk to print first column, second column and one column every four after that

查看:1456
本文介绍了用awk后打印第一列第二列,一列,每四个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,我想提取的第一列,第二列,之后一列,每3。另外,我想摆脱排#2。我如何能做到用awk或类似的?

一个例子:
我有一个文本文件,这样:

  A B C D E F G H I J .. N(头1)
A B C D E F G H I J .. N(头2)
A B C D E F G H I J。N(行1)
A B C D E F G H I J。N(行2)
A B C D E F G H I J .. N(行n)

我试图把它作为

  A B缩略词。N(头1)
A B缩略词。N(行1)
A B缩略词。N(行2)
A B缩略词。N(行n)

感谢。

P.S。我试着在<一提到玩的解决方案href=\"http://stackoverflow.com/questions/5131214/how-to-print-every-4th-column-up-to-nth-column-and-from-n1th-column-to-last-u\">How打印每4列到第n列和第(n + 1)列到最后用awk?但解决方案并没有为我工作


解决方案

  $的awk'NR = 2 {OUT = $ 1!;对于(i = 2; I&LT; = NF; I + = 4)=出了OFS $ I;打印出}'文件
A B缩略词1)
A B缩略词1)
A B缩略词2)
A B缩略词N)

以上输出是凌乱因为 ... 和在你的样品输入,使其无法验证的评论。总是发布实际,可测试输入/输出,而不是描述或这样的其他抽象重新presentation。不要重复在每一行相同的数据,使得它很难映射输出字段输入,所以就更难理解您的需求。这将是一个更有用的例子:

  $猫文件
101 102 103 104 105 106 107 108 109 110 111
201 202 203 204 205 206 207 208 209 210 211
301 302 303 304 305 306 307 308 309 310 311
401 402 403 404 405 406 407 408 409 410 411$ AWK'NR = 2 {OUT = $ 1!;对于(i = 2; I&LT; = NF; I + = 4)=出了OFS $ I;打印出}'文件
101 102 106 110
301 302 306 310
401 402 406 410

I have a text file and I want to extract the first column, second column and one column every 3 after that. Also, I want to get rid of row #2. How can I do that using awk or similar?

An example: I have a text file as such:

A B C D E F G H I J .. N (header 1)
A B C D E F G H I J .. N (header 2)
A B C D E F G H I J .. N (row 1)
A B C D E F G H I J .. N (row 2)
A B C D E F G H I J .. N (row n)

I'm trying to get it as

A B F J .. N (header 1)
A B F J .. N (row 1)
A B F J .. N (row 2)
A B F J .. N (row n)

Thanks.

P.s. I've tried playing with the solutions mentioned in How to print every 4th column up to nth column and from (n+1)th column to last using awk? but the solution doesn't work for me

解决方案

$ awk 'NR!=2{out=$1; for (i=2;i<=NF;i+=4) out = out OFS $i; print out}' file
A B F J 1)
A B F J 1)
A B F J 2)
A B F J n)

The above output is messy because of the ...s and comments in your sample input that make it untestable. Always post ACTUAL, testable input/output, not a description or other abstract representation of such. And don't repeat the same data on every line as that makes it hard to map the output fields to the input and so makes it harder to understand your requirements. This would have been a more useful example:

$ cat file
101 102 103 104 105 106 107 108 109 110 111
201 202 203 204 205 206 207 208 209 210 211
301 302 303 304 305 306 307 308 309 310 311
401 402 403 404 405 406 407 408 409 410 411

$ awk 'NR!=2{out=$1; for (i=2;i<=NF;i+=4) out = out OFS $i; print out}' file
101 102 106 110
301 302 306 310
401 402 406 410

这篇关于用awk后打印第一列第二列,一列,每四个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆