Linux 如何将第三列打印到最后一列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1602035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:47:47  来源:igfitidea点击:

How to print third column to last column?

linuxshellunixmultiple-columns

提问by Amit G

I'm trying to remove the first two columns (of which I'm not interested in) from a DbgView log file. I can't seem to find an example that prints from column 3 onwards until the end of the line. Note that each line has variable number of columns.

我试图从 DbgView 日志文件中删除前两列(我不感兴趣)。我似乎找不到从第 3 列开始打印到行尾的示例。请注意,每行都有可变数量的列。

采纳答案by Marcin

...or a simpler solution: cut -f 3- INPUTFILEjust add the correct delimiter (-d) and you got the same effect.

...或更简单的解决方案:cut -f 3- INPUTFILE只需添加正确的分隔符 (-d) 即可获得相同的效果。

回答by Jonathan Feinberg

awk '{for(i=3;i<=NF;++i)print $i}' 

回答by Eddie Sullivan

Well, you can easily accomplish the same effect using a regular expression. Assuming the separator is a space, it would look like:

好吧,您可以使用正则表达式轻松实现相同的效果。假设分隔符是一个空格,它看起来像:

awk '{ sub(/[^ ]+ +[^ ]+ +/, ""); print }'

回答by Paused until further notice.

Jonathan Feinberg's answer prints each field on a separate line. You could use printfto rebuild the record for output on the same line, but you can also just move the fields a jump to the left.

Jonathan Feinberg的回答将每个字段打印在单独的行上。您可以使用printf在同一行上重建输出记录,但您也可以将字段向左移动一个跳转。

awk '{for (i=1; i<=NF-2; i++) $i = $(i+2); NF-=2; print}' logfile

回答by ghostdog74

awk '{===""}1' file

NB: this method will leave "blanks" in 1,2,3 fields but not a problem if you just want to look at output.

注意:此方法将在 1,2,3 字段中留下“空白”,但如果您只想查看输出,这不是问题。

回答by Wawrzek

What about following line:

下面这行呢:

awk '{===""; print}' file

Based on @ghostdog74 suggestion. Mine should behave better when you filter lines, i.e.:

基于@ghostdog74 的建议。当您过滤线条时,我的应该表现得更好,即:

awk '/^exim4-config/ {=""; print }' file

回答by daisaa

awk '{ print substr(
awk '{for(i=3;i<=NF;++i) printf("%s ",  $i) }'
, index(
awk -v m="\x0a" -v N="3" '{$N=m$N ;print substr(
fromField () { 
awk -v m="\x0a" -v N="" '{$N=m$N; print substr(
$ echo "  bat   bi       iru   lau bost   " | fromField 3
iru   lau bost   
$ echo "  bat   bi       iru   lau bost   " | fromField 2
bi       iru   lau bost 
,index(
awk -v m="\x01" -v N="3" '{$N=m$N ;print substr(
awk '{for (i=4; i<=NF; i++)printf("%c", $i); printf("\n");}'
, index(##代码##,m)+1)}'
,m)+1)}' }
, index(##代码##,m)+1)}'
,)) }'

solution found here:
http://www.linuxquestions.org/questions/linux-newbie-8/awk-print-field-to-end-and-character-count-179078/

解决方案在这里找到:http:
//www.linuxquestions.org/questions/linux-newbie-8/awk-print-field-to-end-and-character-count-179078/

回答by Ross

A bit late here, but none of the above seemed to work. Try this, using printf, inserts spaces between each. I chose to not have newline at the end.

这里有点晚了,但以上似乎都不起作用。试试这个,使用 printf,在每个之间插入空格。我选择最后没有换行符。

##代码##

回答by Robert Vila

##代码##

This chops what is before the given field nr., N, and prints all the rest of the line, including field nr.N and maintaining the original spacing (it does not reformat). It doesn't mater if the string of the field appears also somewhere else in the line, which is the problem with daisaa's answer.

这会截断给定字段 nr., N 之前的内容,并打印该行的所有其余部分,包括字段 nr.N 并保持原始间距(不会重新格式化)。字段的字符串是否也出现在行中的其他地方并不重要,这是 daisaa 的答案的问题。

Define a function:

定义一个函数:

##代码##

And use it like this:

并像这样使用它:

##代码##

Output maintains everything, including trailing spaces

输出维护一切,包括尾随空格

Works well for files where '/n' is the record separator so you don't have that new-line char inside the lines. If you want to use it with other record separators then use:

适用于其中 '/n' 是记录分隔符的文件,因此行内没有换行符。如果要将其与其他记录分隔符一起使用,请使用:

##代码##

for example. Works well with almost all files as long as they don't use hexadecimal char nr. 1 inside the lines.

例如。只要它们不使用十六进制 char nr,几乎所有文件都能很好地工作。1 线内。

回答by Massimo

##代码##

prints records starting from the 4th field to the last field in the same order they were in the original file

按照原始文件中的相同顺序打印从第 4 个字段到最后一个字段的记录