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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:03:44  来源:igfitidea点击:

Get specific line from text file using just shell script

linuxshellunixsh

提问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 lineis a variable which holds your required line number, if you can use headand tail, then it is quite simple:

假设line是一个保存您所需行号的变量,如果您可以使用headand 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-filecontains line continuations, they will be counted as a single line. You can change this behavior by adding -rto the read command. (Which is probably the desired behavior.)

请注意,这将打印逻辑第 5 行。也就是说,如果input-file包含行延续,它们将被视为单行。您可以通过添加-r到读取命令来更改此行为。(这可能是所需的行为。)

回答by faithonour

Best performancemethod

最佳性能方法

sed '5q;d' file

Because sedstops 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 breakout 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

If for example you want to get the lines 10 to 20 of a file you can use each of these two methods:

例如,如果您想获取文件的第 10 到 20 行,您可以使用以下两种方法中的每一种:

head -n 20 york.txt | tail -11

or

或者

sed -n '10,20p' york.txt 

pin above command stands for printing.

p在上面的命令中代表打印。

Here's what you'll see: enter image description here

您将看到以下内容: 在此处输入图片说明

回答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]}"