实现与awk的尾巴 [英] Implement tail with awk
本文介绍了实现与awk的尾巴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,所以我在这里与此AWK code至极奋力应该效仿tail命令
NUM = $ 1;
{
VECT [NR] = $ 0;}
结束{
对于(I = NR-NUM; I< = NR;我++)
打印VECT [$ i]
}
所以我想在这里实现是AWK效仿的tail命令
例如,考虑猫somefile | AWK -f tail.awk 10
768,16打印最后10行的文本文件,有什么建议?
解决方案
为(i = NR-NUM; I< = NR;我++)
打印VECT [$ i]
$
表示一个位置参数。使用只是普通的 I
:
为(i = NR-NUM; I< = NR;我++)
打印VECT [I]
满code为我工作是:
#!的/ usr /斌/的awk -f
开始{
NUM = ARGV [1];
该精氨酸空的,所以AWK#做不跨preT它作为一个文件名。
ARGV [1] =;
}
{
VECT [NR] = $ 0;
}
结束{
对于(I = NR-NUM; I< = NR;我++)
打印VECT [I]
}
您也许应该增加一些code到 END
来处理这种情况时, NR
< NUM
。
Alright so here i am struggling with this awk code wich should emulate the tail command
num=$1;
{
vect[NR]=$0;
}
END{
for(i=NR-num;i<=NR;i++)
print vect[$i]
}
So what i'm trying to achieve here is an tail command emulated by awk for example consider cat somefile | awk -f tail.awk 10 shoud print the last 10 lines of a text file,any suggestions ?
解决方案
for(i=NR-num;i<=NR;i++)
print vect[$i]
$
indicates a positional parameter. Use just plain i
:
for(i=NR-num;i<=NR;i++)
print vect[i]
The full code that worked for me is:
#!/usr/bin/awk -f
BEGIN{
num=ARGV[1];
# Make that arg empty so awk doesn't interpret it as a file name.
ARGV[1] = "";
}
{
vect[NR]=$0;
}
END{
for(i=NR-num;i<=NR;i++)
print vect[i]
}
You should probably add some code to the END
to handle the case when NR
< num
.
这篇关于实现与awk的尾巴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文