Linux 使用awk打印从第n到最后的所有列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2961635/
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 20:05:49  来源:igfitidea点击:

Using awk to print all columns from the nth to the last

linuxawk

提问by Andy

This line worked until I had whitespace in the second field.

这条线一直有效,直到我在第二个字段中有空格。

svn status | grep '\!' | gawk '{print ;}' > removedProjs

is there a way to have awk print everything in $2 or greater? ($3, $4.. until we don't have anymore columns?)

有没有办法让 awk 以 2 美元或更高的价格打印所有内容?(3 美元,4 美元 .. 直到我们不再有列?)

I suppose I should add that I'm doing this in a Windows environment with Cygwin.

我想我应该补充一点,我是在带有 Cygwin 的 Windows 环境中执行此操作的。

采纳答案by zed_0xff

will print all but very first column:

将打印除第一列之外的所有内容:

awk '{=""; print 
awk '{==""; print 
awk '{out=""; for(i=2;i<=NF;i++){out=out" "$i}; print out}'
}' somefile
}' somefile

will print all but two first columns:

将打印除第一列之外的所有列:

awk '{for(i=2;i<=NF;i++){printf "%s ", $i}; printf "\n"}'

回答by VeeArr

You could use a for-loop to loop through printing fields $2 through $NF (built-in variable that represents the number of fields on the line).

您可以使用 for 循环来循环打印字段 $2 到 $NF(表示行上字段数的内置变量)。

Edit: Since "print" appends a newline, you'll want to buffer the results:

编辑:由于“打印”附加换行符,您需要缓冲结果:

awk '{print substr(
awk '{out=; for(i=3;i<=NF;i++){out=out" "$i}; print out}'
,length()+1);}' < file

Alternatively, use printf:

或者,使用 printf:

 svn status |  grep '\!' | cut -d\  -f2-

回答by whaley

Would this work?

这行得通吗?

echo "1 2 3 4 5 6" | awk '{ $NF = ""; print 
ls -lthr | awk '{out=; for(i=7;i<=NF;i++){out=out" "$i}; print out}'
}'

It leaves some whitespace in front though.

不过,它在前面留下了一些空白。

回答by Wim

ls -lthr | awk '{ORS=" "; for(i=6;i<=NF;i++) print $i;print "\n"}'

My answer is based on the one of VeeArr, but I noticed it started with a white space before it would print the second column (and the rest). As I only have 1 reputation point, I can't comment on it, so here it goes as a new answer:

我的答案基于VeeArr 之一,但我注意到它在打印第二列(和其余列)之前以空​​格开头。由于我只有 1 个声望点,我无法对此发表评论,因此这里作为一个新答案:

start with "out" as the second column and then add all the other columns (if they exist). This goes well as long as there is a second column.

以“out”作为第二列开始,然后添加所有其他列(如果存在)。只要有第二列,这就会顺利进行。

回答by Joshua Goldberg

There's a duplicate question with a simpler answerusing cut:

有一个重复的问题,使用 cut有一个更简单的答案

ls -l | awk '{sub(/[^ ]+ /, ""); print 
awk -F" " '{ for (i=4; i<=NF; i++) print $i }'
}'

-dspecifies the delimeter (space), -fspecifies the list of columns (all starting with the 2nd)

-d指定分隔符(空格)-f指定列的列表(都从第 2 个开始)

回答by Kaushal Jha

@m=`ls -ltr dir | grep ^d | awk '{print $6,$7,$8,$9}'`;
foreach $i (@m)
{
        print "$i\n";

}

this one uses awk to print all except the last field

这个使用 awk 打印除最后一个字段之外的所有内容

回答by Manuel Parra

This is what I preferred from all the recommendations:

这是我从所有建议中更喜欢的:

Printing from the 6th to last column.

从第 6 列到最后一列打印。

##代码##

or

或者

##代码##

回答by savvadia

Printing out columns starting from #2 (the output will have no trailing space in the beginning):

打印从 #2 开始的列(输出开头没有尾随空格):

##代码##

回答by koullislp

I personally tried all the answers mentioned above, but most of them were a bit complex or just not right. The easiest way to do it from my point of view is:

我个人尝试了上面提到的所有答案,但大多数答案都有些复杂或不正确。从我的角度来看,最简单的方法是:

##代码##
  1. Where -F" " defines the delimiter for awk to use. In my case is the whitespace, which is also the default delimiter for awk. This means that -F" " can be ignored.

  2. Where NF defines the total number of fields/columns. Therefore the loop will begin from the 4th field up to the last field/column.

  3. Where $N retrieves the value of the Nth field. Therefore print $i will print the current field/column based based on the loop count.

  1. 其中 -F" " 定义了 awk 使用的分隔符。在我的例子中是空格,它也是 awk 的默认分隔符。这意味着 -F" " 可以被忽略。

  2. 其中 NF 定义字段/列的总数。因此循环将从第 4 个字段开始直到最后一个字段/列。

  3. 其中 $N 检索第 N 个字段的值。因此 print $i 将根据循环计数打印当前字段/列。

回答by pkm

Perl:

珀尔:

##代码##