has*_*ang 24 linux bash shell command-line command
在Linux中有没有办法要求Head或Tail但是要忽略记录的额外偏移量.
例如,如果文件example.lst
包含以下内容:
row01
row02
row03
row04
row05
Run Code Online (Sandbox Code Playgroud)
我使用head -n3 example.lst
我可以获得行1 - 3但是如果我想让它跳过第一行并得到行2 - 4怎么办?
我问,因为一些命令有一个标题,这在搜索结果中可能是不可取的.例如,du -h ~ --max-depth 1 | sort -rh
将返回主目录中按降序排序的所有文件夹的目录大小,但会将当前目录追加到结果集的顶部(即~
).
Head和Tail手册页似乎没有任何偏移参数,所以可能有某种range
命令可以指定所需的行:例如range 2-10
或者什么?
tha*_*guy 42
来自man tail
:
-n, --lines=K
output the last K lines, instead of the last 10;
or use -n +K to output lines starting with the Kth
Run Code Online (Sandbox Code Playgroud)
因此,您可以使用... | tail -n +2 | head -n 3
从第2行开始获得3行.
非头/尾方法包括sed -n "2,4p"
和awk "NR >= 2 && NR <= 4"
.
要获取 2 到 4(包括)之间的行,您可以使用:
head -n4 example.lst | tail -n+2
Run Code Online (Sandbox Code Playgroud)
或者
head -n4 example.lst | tail -n3
Run Code Online (Sandbox Code Playgroud)