awk +需要打印除 $1 和 $2 之外的所有内容(所有其余字段)
我有以下文件,我需要通过 awk
文件打印除 $1
和 $2
之外的所有内容:
INFORMATION DATA 12 33 55 33 66 43
INFORMATION DATA 45 76 44 66 77 33
INFORMATION DATA 77 83 56 77 88 22
...
所需的输出:
12 33 55 33 66 43
45 76 44 66 77 33
77 83 56 77 88 22
...
I have the following file and I need to print everything except $1
and $2
by awk
File:
INFORMATION DATA 12 33 55 33 66 43
INFORMATION DATA 45 76 44 66 77 33
INFORMATION DATA 77 83 56 77 88 22
...
the desirable output:
12 33 55 33 66 43
45 76 44 66 77 33
77 83 56 77 88 22
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好吧,考虑到你的数据,削减应该足够了:
Well, given your data, cut should be sufficient:
尽管与 yael 的预期输出相比,它在每行的开头添加了一个额外的空格,但这里是一个比之前建议的更短、更简单的基于 awk 的解决方案:
甚至:
Although it adds an extra space at the beginning of each line compared to yael's expected output, here is a shorter and simpler awk based solution than the previously suggested ones:
or even:
danbens 答案在结果字符串的末尾留下一个空格。所以正确的方法是:
danbens answer leaves a whitespace at the end of the resulting string. so the correct way to do it would be:
如果前两个词不改变,最简单的可能是:
If the first two words don't change, probably the simplest thing would be:
这是另一种
awk
解决方案,它比cut
解决方案更灵活,并且比其他awk
解决方案更短。假设您的分隔符是单个空格(如果不是,请根据需要修改正则表达式):Here's another
awk
solution, that's more flexible than thecut
one and is shorter than the otherawk
ones. Assuming your separators are single spaces (modify the regex as necessary if they are not):如果 Perl 是一个选项:
使用这些命令行选项:
-n
循环输入文件的每一行,不自动打印-l
在处理之前删除换行符,然后将其添加回来-a
自动拆分模式 – 将输入行拆分到@F
数组中。默认按空格分割-e
执行 Perl 代码splice @F,0,2
干净地从@F
数组中删除第 0 列和第 1 列join " ",@F
连接@F
数组的元素,每个元素之间使用空格csv 输入文件的变体:
这使用
- F
带逗号的字段分隔符选项If Perl is an option:
These command-line options are used:
-n
loop around every line of the input file, do not automatically print it-l
removes newlines before processing, and adds them back in afterwards-a
autosplit mode – split input lines into the@F
array. Defaults to splitting on whitespace-e
execute the perl codesplice @F,0,2
cleanly removes columns 0 and 1 from the@F
arrayjoin " ",@F
joins the elements of the@F
array, using a space in-between each elementVariation for csv input files:
This uses the
-F
field separator option with a comma