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

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


当前回答

已经有很多好答案了。我个人喜欢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

其他回答

这个问题被标记为Bash,下面是Bash(≥4)的方法:使用带有-s(跳过)和-n(计数)选项的mapfile。

如果需要获取文件文件的第42行:

mapfile -s 41 -n 1 ary < file

此时,您将得到一个数组ary,其中的字段包含文件行(包括尾部换行符),我们跳过了前41行(-s 41),并在读取一行(-n 1)后停止。这真的是第42行。要打印出来:

printf '%s' "${ary[0]}"

如果您需要一系列行,请说出范围42–666(含),并说您不想自己计算,然后将它们打印在标准输出上:

mapfile -s $((42-1)) -n $((666-42+1)) ary < file
printf '%s' "${ary[@]}"

如果您也需要处理这些行,那么存储尾随换行符并不太方便。在这种情况下,使用-t选项(trim):

mapfile -t -s $((42-1)) -n $((666-42+1)) ary < file
# do stuff
printf '%s\n' "${ary[@]}"

你可以让一个函数为你做这件事:

print_file_range() {
    # $1-$2 is the range of file $3 to be printed to stdout
    local ary
    mapfile -s $(($1-1)) -n $(($2-$1+1)) ary < "$3"
    printf '%s' "${ary[@]}"
}

没有外部命令,只有Bash内置命令!

哇,所有的可能性!

试试看:

sed -n "${lineNum}p" $file

或者根据您的Awk版本选择其中之一:

awk  -vlineNum=$lineNum 'NR == lineNum {print $0}' $file
awk -v lineNum=4 '{if (NR == lineNum) {print $0}}' $file
awk '{if (NR == lineNum) {print $0}}' lineNum=$lineNum $file

(您可能需要尝试nawk或gawk命令)。

是否有一种工具只打印特定的行?不是标准工具之一。然而,sed可能是最接近和最简单的用法。

已经有很多好答案了。我个人喜欢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

如果有多行由\n分隔(通常为新行)。您也可以使用“cut”:

echo "$data" | cut -f2 -d$'\n'

您将从文件中获得第二行-f3给你第三行。

要使用sed和变量作为行号打印第n行,请执行以下操作:

a=4
sed -e $a'q:d' file

这里的“-e”标志用于将脚本添加到要执行的命令中。