这一行一直工作到第二个字段中出现空白。
svn status | grep '\!' | gawk '{print $2;}' > removedProjs
有没有办法让awk打印所有2美元或更大的东西?(3、4美元. .直到我们不再有专栏了?)
我想我应该补充一点,我正在使用Cygwin在Windows环境中执行此操作。
这一行一直工作到第二个字段中出现空白。
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 -F" " '{ for (i=4; i<=NF; i++) print $i }'
其中-F" "定义了awk使用的分隔符。在我的例子中是空格,它也是awk的默认分隔符。这意味着-F" "可以被忽略。 其中NF定义字段/列的总数。因此,循环将从第4个字段开始,一直到最后一个字段/列。 其中$N检索第N个字段的值。因此,print $i将根据循环计数打印当前字段/列。
Perl:
@m=`ls -ltr dir | grep ^d | awk '{print \$6,\$7,\$8,\$9}'`;
foreach $i (@m)
{
print "$i\n";
}
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
这样能行吗?
awk '{print substr($0,length($1)+1);}' < file
它在前面留下了一些空白。
awk '{out=$2; for(i=3;i<=NF;i++){out=out" "$i}; print out}'
我的答案是基于VeeArr的答案,但我注意到它在打印第二列(以及其余部分)之前以空白开始。因为我只有1个声望点,所以我不能评论它,所以这是一个新的答案:
以“out”作为第二列开始,然后添加所有其他列(如果存在)。只要有第二列,这就很好。