这一行一直工作到第二个字段中出现空白。
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 '{ for(i=3; i<=NF; ++i) printf $i""FS; print "" }'
Lauhub在这里提出了这个正确、简单、快速的解决方案
其他回答
如果你想要格式化文本,用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 |
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
$
在这里给出的所有其他答案以及在相关问题中给出的各种可能的FS值都以各种方式失败。有些在开头和/或结尾留下空白,有些将每个FS转换为OFS,有些依赖于仅当FS为默认值时才适用的语义,有些依赖于在括号表达式中否定FS,这将在给定多字符FS时失败,等等。
为了对任何FS都健壮地做到这一点,使用GNU awk的第4个参数split():
$ cat tst.awk
{
split($0,flds,FS,seps)
for ( i=n; i<=NF; i++ ) {
printf "%s%s", flds[i], seps[i]
}
print ""
}
$ printf 'a b c d\n' | awk -v n=3 -f tst.awk c d $ printf ' a b c d\n' | awk -v n=3 -f tst.awk c d $ printf ' a b c d\n' | awk -v n=3 -F'[ ]' -f tst.awk b c d $ printf ' a b c d\n' | awk -v n=3 -F'[ ]+' -f tst.awk b c d $ printf 'a###b###c###d\n' | awk -v n=3 -F'###' -f tst.awk c###d $ printf '###a###b###c###d\n' | awk -v n=3 -F'###' -f tst.awk b###c###d Note that I'm using split() above because it's 3rg arg is a field separator, not just a regexp like the 2nd arg to match(). The difference is that field separators have additional semantics to regexps such as skipping leading and/or trailing blanks when the separator is a single blank char - if you wanted to use a while(match()) loop or any form of *sub() to emulate the above then you'd need to write code to implement those semantics whereas split() already implements them for you.
我想把建议的答案扩展到字段可能由几个空格分隔的情况——我想这就是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-
打印从#2开始的列(输出在开始时没有尾随空格):
ls -l | awk '{sub(/[^ ]+ /, ""); print $0}'