这一行一直工作到第二个字段中出现空白。
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环境中执行此操作。
当前回答
这样能行吗?
awk '{print substr($0,length($1)+1);}' < file
它在前面留下了一些空白。
其他回答
ls -la | awk '{o=$1" "$3; for (i=5; i<=NF; i++) o=o" "$i; print o }'
这个答案还不错,但是自然的间隔消失了。 请将它与这个进行比较:
ls -la | cut -d\ -f4-
然后你就会看到区别了。
甚至ls -la | awk '{$1=$2="";打印},这是基于迄今为止投票选出的最佳答案,即不保留格式。
因此,我将使用以下,它还允许显式选择列在开始:
ls -la | cut -d\ -f1,4-
注意,每个空格也计算列,例如在下面,列1和列3是空的,2是INFO, 4是:
$ echo " INFO 2014-10-11 10:16:19 main " | cut -d\ -f1,3
$ echo " INFO 2014-10-11 10:16:19 main " | cut -d\ -f2,4
INFO 2014-10-11
$
您可以使用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"}'
这样能行吗?
awk '{print substr($0,length($1)+1);}' < file
它在前面留下了一些空白。
我想把建议的答案扩展到字段可能由几个空格分隔的情况——我想这就是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解决方案都不满意,因为我想提取前几列,然后打印其余的,所以我转而使用perl。下面的代码提取了前两列,并按原样显示其余部分:
echo -e "a b c d\te\t\tf g" | \
perl -ne 'my @f = split /\s+/, $_, 3; printf "first: %s second: %s rest: %s", @f;'
与Chris Koknat的perl解决方案相比,优点是实际上只从输入字符串中分离出前n个元素;字符串的其余部分根本没有被分割,因此保持完整。我的示例混合使用空格和制表符来演示这一点。
要更改应该提取的列的数量,请将示例中的3替换为n+1。