这一行一直工作到第二个字段中出现空白。

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

有没有办法让awk打印所有2美元或更大的东西?(3、4美元. .直到我们不再有专栏了?)

我想我应该补充一点,我正在使用Cygwin在Windows环境中执行此操作。


当前回答

我想把建议的答案扩展到字段可能由几个空格分隔的情况——我想这就是OP不使用cut的原因。

我知道OP问了关于awk的问题,但sed方法在这里可以工作(例如从第5列打印到最后一列):

pure sed approach sed -r 's/^\s*(\S+\s+){4}//' somefile Explanation: s/// is the standard command to perform substitution ^\s* matches any consecutive whitespace at the beginning of the line \S+\s+ means a column of data (non-whitespace chars followed by whitespace chars) (){4} means the pattern is repeated 4 times. sed and cut sed -r 's/^\s+//; s/\s+/\t/g' somefile | cut -f5- by just replacing consecutive whitespaces by a single tab; tr and cut: tr can also be used to squeeze consecutive characters with the -s option. tr -s [:blank:] <somefile | cut -d' ' -f5-

其他回答

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

Lauhub在这里提出了这个正确、简单、快速的解决方案

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

ls -l | awk '{sub(/[^ ]+ /, ""); print $0}'

zed_0xff投票最多的答案不适合我。

我有一个日志,其中$5与IP地址可以是更多的文本或没有文本。我需要从IP地址到行尾的所有内容,如果有任何在$5之后。在我的例子中,这实际上是在一个awk程序中,而不是awk一行程序,因此awk必须解决这个问题。当我试图删除前4个字段使用zed_0xff提出的解决方案:

echo "  7 27.10.16. Thu 11:57:18 37.244.182.218" | awk '{$1=$2=$3=$4=""; printf "[%s]\n", $0}'

它吐出错误和无用的回应(我添加了[..]以示范):

[    37.244.182.218 one two three]

甚至有人建议将substr与这个错误的答案结合起来,但这只会使事情复杂化。它没有提供任何改进。

相反,如果列的宽度是固定的,直到需要切割点和awk,正确答案是:

echo "  7 27.10.16. Thu 11:57:18 37.244.182.218" | awk '{printf "[%s]\n", substr($0,28)}'

它产生所需的输出:

[37.244.182.218 one two three]

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

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

-d指定delimeter(空格),-f指定列列表(所有列都从第2个开始)

如果需要用任意delimeter打印特定列:

awk '{print $3 "  " $4}'

同# 3 # 4

awk '{print $3 "anything" $4}'

与3anythingcol # 4

因此,如果您在一列中有空格,它将是两列,但您可以使用任何分隔符连接它或不使用它。