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

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

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

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


当前回答

您可以使用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"}'

其他回答

echo "1 2 3 4 5 6" | awk '{ $NF = ""; print $0}'

它使用awk打印除最后一个字段以外的所有字段

Perl的解决方案:

perl -lane 'splice @F,0,1; print join " ",@F' file

使用这些命令行选项:

-n循环输入文件的每一行,不自动打印每一行 -l在处理之前删除换行符,并在处理之后将它们添加回去 -a autosplit mode -将输入行分割到@F数组中。默认为空格分割 -e执行perl代码

splice @F,0,1从@F数组中清除第0列

join " ",@F连接@F数组的元素,在每个元素之间使用一个空格


Python的解决方案:

[sys.stdout.]写(' ' . join (line.split () [1:]) + ' \ n ')系统的线。Stdin]" <文件

Awk示例在这里看起来很复杂,这里是简单的Bash shell语法:

command | while read -a cols; do echo ${cols[@]:1}; done

1是第n列,从0开始计数。


例子

给定文件(in.txt)的内容:

c1
c1 c2
c1 c2 c3
c1 c2 c3 c4
c1 c2 c3 c4 c5

输出如下:

$ while read -a cols; do echo ${cols[@]:1}; done < in.txt 

c2
c2 c3
c2 c3 c4
c2 c3 c4 c5

以下是我在所有推荐中更喜欢的:

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

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

or

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

我想把建议的答案扩展到字段可能由几个空格分隔的情况——我想这就是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-