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

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

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

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


当前回答

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

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

其他回答

如果你想要格式化文本,用echo链接你的命令并使用$0打印最后一个字段。

例子:

for i in {8..11}; do
   s1="$i"
   s2="str$i"
   s3="str with spaces $i"
   echo -n "$s1 $s2" | awk '{printf "|%3d|%6s",$1,$2}'
   echo -en "$s3" | awk '{printf "|%-19s|\n", $0}'
done

打印:

|  8|  str8|str with spaces 8  |
|  9|  str9|str with spaces 9  |
| 10| str10|str with spaces 10 |
| 11| str11|str with spaces 11 |
awk '{out=$2; for(i=3;i<=NF;i++){out=out" "$i}; print out}'

我的答案是基于VeeArr的答案,但我注意到它在打印第二列(以及其余部分)之前以空白开始。因为我只有1个声望点,所以我不能评论它,所以这是一个新的答案:

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

打印所有列:

awk '{print $0}' somefile

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

awk '{$1=""; print $0}' somefile

打印除前两列以外的所有内容:

awk '{$1=$2=""; print $0}' somefile

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]" <文件

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