Linux 仅使用 shell 脚本从文本文件中获取特定行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19327556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get specific line from text file using just shell script
提问by GangstaGraham
I am trying to get a specific line from a text file.
我正在尝试从文本文件中获取特定行。
So far, online I have only seen stuff like sed, (I can only use the sh -not bash or sed or anything like that). I need to do this only using a basic shell script.
到目前为止,我在网上只看到过像 sed 这样的东西(我只能使用 sh -not bash 或 sed 或类似的东西)。我只需要使用一个基本的 shell 脚本就可以做到这一点。
cat file | while read line
do
#do something
done
I know how to iterate through lines, as shown above, but what if I just need to get the contents of a particular line
我知道如何遍历行,如上所示,但是如果我只需要获取特定行的内容怎么办
采纳答案by Kent
sed:
sed:
sed '5!d' file
awk:
awk:
awk 'NR==5' file
回答by micromoses
Assuming line
is a variable which holds your required line number, if you can use head
and tail
, then it is quite simple:
假设line
是一个保存您所需行号的变量,如果您可以使用head
and tail
,那么它非常简单:
head -n $line file | tail -1
If not, this should work:
如果没有,这应该有效:
x=0
want=5
cat lines | while read line; do
x=$(( x+1 ))
if [ $x -eq "$want" ]; then
echo $line
break
fi
done
回答by William Pursell
The standard way to do this sort of thing is to use external tools. Disallowing the use of external tools while writing a shell script is absurd. However, if you really don't want to use external tools, you can print line 5 with:
做这种事情的标准方法是使用外部工具。在编写 shell 脚本时禁止使用外部工具是荒谬的。但是,如果您真的不想使用外部工具,则可以使用以下命令打印第 5 行:
i=0; while read line; do test $((++i)) = 5 && echo "$line"; done < input-file
Note that this will print logical line 5. That is, if input-file
contains line continuations, they will be counted as a single line. You can change this behavior by adding -r
to the read command. (Which is probably the desired behavior.)
请注意,这将打印逻辑第 5 行。也就是说,如果input-file
包含行延续,它们将被视为单行。您可以通过添加-r
到读取命令来更改此行为。(这可能是所需的行为。)
回答by faithonour
Best performancemethod
最佳性能方法
sed '5q;d' file
Because sed
stops reading any lines after the 5th one
因为sed
在第 5 行之后停止阅读任何行
Update experiment from Mr. Roger Dueck
Roger Dueck 先生的更新实验
I installed wcanadian-insane (6.6MB) and compared sed -n 1p /usr/share/dict/words and sed '1q;d' /usr/share/dict/words using the time command; the first took 0.043s, the second only 0.002s, so using 'q' is definitely a performance improvement!
我安装了 wcanadian-insane (6.6MB) 并使用 time 命令比较了 sed -n 1p /usr/share/dict/words 和 sed '1q;d' /usr/share/dict/words ;第一个用了 0.043s,第二个只用了 0.002s,所以使用 'q' 绝对是性能提升!
回答by tripleee
In parallel with William Pursell's answer, here is a simple construct which should work even in the original v7 Bourne shell (and thus also places where Bash is not available).
与William Pursell 的回答并行,这里有一个简单的结构,即使在原始的 v7 Bourne shell 中也应该可以工作(因此在 Bash 不可用的地方)。
i=0
while read line; do
i=`expr "$i" + 1`
case $i in 5) echo "$line"; break;; esac
done <file
Notice also the optimization to break
out of the loop when we have obtained the line we were looking for.
还要注意break
当我们获得我们正在寻找的行时对循环外的优化。
回答by Nomas Prime
You could use sed -n 5p file
.
你可以使用sed -n 5p file
.
You can also get a range, e.g., sed -n 5,10p file
.
您还可以获得一个范围,例如,sed -n 5,10p file
。
回答by dagelf
Easy with perl! If you want to get line 1, 3 and 5 from a file, say /etc/passwd:
使用 perl 很容易!如果要从文件中获取第 1、3 和 5 行,请说 /etc/passwd:
perl -e 'while(<>){if(++$l~~[1,3,5]){print}}' < /etc/passwd
回答by Mona Jalal
回答by Oder
line=5; prep=`grep -ne ^ file.txt | grep -e ^$line:`; echo "${prep#$line:}"
回答by cpp_guy_who_does_gfx
I didn't particularly like any of the answers.
我不是特别喜欢任何一个答案。
Here is how I did it.
这是我如何做到的。
# Convert the file into an array of strings
lines=(`cat "foo.txt"`)
# Print out the lines via array index
echo "${lines[0]}"
echo "${lines[1]}"
echo "${lines[5]}"