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

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-

其他回答

如果你正在使用Bash,你可以使用尽可能多的“x”作为你希望丢弃的元素,如果它们没有转义,它会忽略多个空格。

while read x b; do echo "$b"; done < filename

这样能行吗?

awk '{print substr($0,length($1)+1);}' < file

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

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

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

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

编辑: 由于"print"附加了换行符,你需要缓冲结果:

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

或者,使用printf:

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

Perl:

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

}