我想说输出文件的第 5 - 10 行,作为传入的参数。
我怎样才能使用head
and tail
去做这个?
where firstline = $2
and lastline = $3
and filename = $1
.
运行它应该如下所示:
./lines.sh filename firstline lastline
head -n XX # <-- print first XX lines
tail -n YY # <-- print last YY lines
如果您想要从 20 到 30 的行,则意味着您需要从 20 开始到 30 结束的 11 行:
head -n 30 file | tail -n 11
#
# first 30 lines
# last 11 lines from those previous 30
也就是说,你首先获得第一30
行,然后选择最后一个11
(那是,30-20+1
).
所以在你的代码中它将是:
head -n $3 $1 | tail -n $(( $3-$2 + 1 ))
基于firstline = $2
, lastline = $3
, filename = $1
head -n $lastline $filename | tail -n $(( $lastline -$firstline + 1 ))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)