有没有一种“规范”的方法?我一直在使用head-n|tail-1来完成这个任务,但我一直在想是否有一个Bash工具专门从文件中提取一行(或一系列行)。

我所说的“规范”是指一个主要功能就是这样做的程序。


当前回答

有了awk,速度相当快:

awk 'NR == num_line' file

如果为true,则执行awk的默认行为:{print$0}。


替代版本

如果您的文件恰好很大,最好在读取所需的行后退出。这样可以节省CPU时间请参见答案末尾的时间比较。

awk 'NR == num_line {print; exit}' file

如果要从bash变量中给出行号,可以使用:

awk 'NR == n' n=$num file
awk -v n=$num 'NR == n' file   # equivalent

查看使用exit节省了多少时间,特别是如果该行恰好位于文件的第一部分:

# Let's create a 10M lines file
for ((i=0; i<100000; i++)); do echo "bla bla"; done > 100Klines
for ((i=0; i<100; i++)); do cat 100Klines; done > 10Mlines

$ time awk 'NR == 1234567 {print}' 10Mlines
bla bla

real    0m1.303s
user    0m1.246s
sys 0m0.042s
$ time awk 'NR == 1234567 {print; exit}' 10Mlines
bla bla

real    0m0.198s
user    0m0.178s
sys 0m0.013s

因此,两者的差异是0.198秒对1.303秒,大约快了6倍。

其他回答

我将上面的一些答案放入了一个简短的bash脚本中,您可以将其放入名为get.sh的文件中,并链接到/usr/local/bin/get(或您喜欢的任何其他名称)。

#!/bin/bash
if [ "${1}" == "" ]; then
    echo "error: blank line number";
    exit 1
fi
re='^[0-9]+$'
if ! [[ $1 =~ $re ]] ; then
    echo "error: line number arg not a number";
    exit 1
fi
if [ "${2}" == "" ]; then
    echo "error: blank file name";
    exit 1
fi
sed "${1}q;d" $2;
exit 0

确保它可通过

$ chmod +x get

将其链接以使其在PATH上可用

$ ln -s get.sh /usr/local/bin/get

已经有很多好答案了。我个人喜欢awk。为了方便起见,如果您使用bash,只需将以下内容添加到~/.bash_profile中即可。下次登录时(或者如果您在本次更新后获取.bash_profile的源代码),您将有一个新的漂亮的“第n”函数可用于管道传输文件。

执行此命令或将其放入~/.bash_profile(如果使用bash)并重新打开bash(或执行源~/.bach_profile)

# print just the nth piped in line
nth () { awk -vlnum=${1} 'NR==lnum {print; exit}'; } 

然后,要使用它,只需通过管道。例如:

$ yes line | cat -n | nth 5
     5  line

对于一个巨大的文件来说,头部和尾部的管道会很慢。我建议您这样做:

sed 'NUMq;d' file

其中NUM是要打印的行数;例如,sed’10q;d'文件以打印文件的第10行。

说明:

当行号为NUM时,NUMq将立即退出。

d将删除该行而不是打印该行;这在最后一行被禁止,因为q会导致退出时跳过脚本的其余部分。

如果变量中有NUM,则需要使用双引号而不是单引号:

sed "${NUM}q;d" file
sed -n '2p' < file.txt

将打印第二行

sed -n '2011p' < file.txt

2011线

sed -n '10,33p' < file.txt

第10行到第33行

sed -n '1p;3p' < file.txt

第1和第3行

等等

对于使用sed添加行,您可以选中此项:

sed:在某个位置插入一行

大文件的最快解决方案始终是尾部|头部,前提是两个距离:

从文件开头到开始行。我们称之为S从最后一行到文件结尾的距离。是E吗

是已知的。然后,我们可以使用这个:

mycount="$E"; (( E > S )) && mycount="+$S"
howmany="$(( endline - startline + 1 ))"
tail -n "$mycount"| head -n "$howmany"

多少只是所需的行数。

更多详情请参见https://unix.stackexchange.com/a/216614/79743