这一行一直工作到第二个字段中出现空白。
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-
其他回答
以下是我在所有推荐中更喜欢的:
从第六列到最后一列打印。
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"}'
awk '{out=$2; for(i=3;i<=NF;i++){out=out" "$i}; print out}'
我的答案是基于VeeArr的答案,但我注意到它在打印第二列(以及其余部分)之前以空白开始。因为我只有1个声望点,所以我不能评论它,所以这是一个新的答案:
以“out”作为第二列开始,然后添加所有其他列(如果存在)。只要有第二列,这就很好。
我想把建议的答案扩展到字段可能由几个空格分隔的情况——我想这就是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-
这让我非常恼火,我坐下来写了一个类似cut的字段规范解析器,用GNU Awk 3.1.7测试。
首先,创建一个新的Awk库脚本pfcut。
sudo nano /usr/share/awk/pfcut
然后,粘贴下面的脚本,并保存。之后,是这样的用法:
$ echo "t1 t2 t3 t4 t5 t6 t7" | awk -f pfcut --source '/^/ { pfcut("-4"); }'
t1 t2 t3 t4
$ echo "t1 t2 t3 t4 t5 t6 t7" | awk -f pfcut --source '/^/ { pfcut("2-"); }'
t2 t3 t4 t5 t6 t7
$ echo "t1 t2 t3 t4 t5 t6 t7" | awk -f pfcut --source '/^/ { pfcut("-2,4,6-"); }'
t1 t2 t4 t6 t7
为了避免键入所有这些,我想最好的方法是(参见其他方法)在启动时自动加载用户函数?- Unix & Linux Stack Exchange)为~/.bashrc添加别名;例如:
$ echo "alias awk-pfcut='awk -f pfcut --source'" >> ~/.bashrc
$ source ~/.bashrc # refresh bash aliases
... 然后你可以调用:
$ echo "t1 t2 t3 t4 t5 t6 t7" | awk-pfcut '/^/ { pfcut("-2,4,6-"); }'
t1 t2 t4 t6 t7
以下是pfcut脚本的源代码:
# pfcut - print fields like cut
#
# sdaau, GNU GPL
# Nov, 2013
function spfcut(formatstring)
{
# parse format string
numsplitscomma = split(formatstring, fsa, ",");
numspecparts = 0;
split("", parts); # clear/initialize array (for e.g. `tail` piping into `awk`)
for(i=1;i<=numsplitscomma;i++) {
commapart=fsa[i];
numsplitsminus = split(fsa[i], cpa, "-");
# assume here a range is always just two parts: "a-b"
# also assume user has already sorted the ranges
#print numsplitsminus, cpa[1], cpa[2]; # debug
if(numsplitsminus==2) {
if ((cpa[1]) == "") cpa[1] = 1;
if ((cpa[2]) == "") cpa[2] = NF;
for(j=cpa[1];j<=cpa[2];j++) {
parts[numspecparts++] = j;
}
} else parts[numspecparts++] = commapart;
}
n=asort(parts); outs="";
for(i=1;i<=n;i++) {
outs = outs sprintf("%s%s", $parts[i], (i==n)?"":OFS);
#print(i, parts[i]); # debug
}
return outs;
}
function pfcut(formatstring) {
print spfcut(formatstring);
}
我对这里提供的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。