更新时间:2023-08-26 18:26:58
使用head
和tail
Using head
and tail
$ head -2 inputFile | tail -1
5 6 7 8
OR
通用版本
$ line=2
$ head -"$line" input | tail -1
5 6 7 8
使用sed
$ sed -n '2 p' input
5 6 7 8
$ sed -n "$line p" input
5 6 7 8
它做什么?
-n
禁止正常打印图案空间.
-n
suppresses normal printing of pattern space.
'2 p'
指定行号,2
或($line
用于更常规),p
命令以打印当前模式空间
'2 p'
specifies the line number, 2
or ($line
for more general), p
commands to print the current patternspace
input
输入文件
修改
要使输出到某个变量,请使用一些命令替换技术.
To get the output to some variable use some command substitution techniques.
$ content=`sed -n "$line p" input`
$ echo $content
5 6 7 8
OR
$ content=$(sed -n "$line p" input)
$ echo $content
5 6 7 8
获取bash数组的输出
To obtain the output to a bash array
$ content= ( $(sed -n "$line p" input) )
$ echo ${content[0]}
5
$ echo ${content[1]}
6
使用awk
也许awk
解决方案可能看起来像
Perhaps an awk
solution might look like
$ awk -v line=$line 'NR==line' input
5 6 7 8
感谢弗雷德里克·皮尔(Fredrik Pihl)的建议.
Thanks to Fredrik Pihl for the suggestion.