使用shell脚本从文本文件中获取特定的行

时间:2021-08-30 00:00:07

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 -而不能使用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

正如上面所示,我知道如何遍历行,但是如果我只需要获取特定行的内容,又该怎么办呢

9 个解决方案

#1


126  

sed:

对话:

sed '5!d' file

awk:

awk:

awk 'NR==5' file

#2


14  

Assuming line is a variable which holds your required line number, if you can use head and tail, then it is quite simple:

假设行是一个包含所需行号的变量,如果可以使用head和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

#3


8  

You could use sed -n 5p file.

您可以使用sed - n5p文件。

You can also get a range, e.g., sed -n 5,10p file.

您还可以得到一个范围,例如,sed - n5,10p文件。

#4


4  

I usually use this for this purpose:

我通常以此为目的:

sed '5q;d' file

sed 5 q;d的文件

#5


3  

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 

p in above command stands for printing.

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

Here's what you'll see: 使用shell脚本从文本文件中获取特定的行

你将会看到:

#6


2  

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。也就是说,如果输入文件包含行延续,它们将被算作一行。您可以通过向read命令添加-r来更改此行为。(这可能是我们希望的行为。)

#7


0  

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.

还要注意优化,当我们得到我们要查找的线时,要跳出循环。

#8


0  

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

#9


0  

line=5; prep=`grep -ne ^ file.txt | grep -e ^$line:`; echo "${prep#$line:}"

#1


126  

sed:

对话:

sed '5!d' file

awk:

awk:

awk 'NR==5' file

#2


14  

Assuming line is a variable which holds your required line number, if you can use head and tail, then it is quite simple:

假设行是一个包含所需行号的变量,如果可以使用head和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

#3


8  

You could use sed -n 5p file.

您可以使用sed - n5p文件。

You can also get a range, e.g., sed -n 5,10p file.

您还可以得到一个范围,例如,sed - n5,10p文件。

#4


4  

I usually use this for this purpose:

我通常以此为目的:

sed '5q;d' file

sed 5 q;d的文件

#5


3  

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 

p in above command stands for printing.

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

Here's what you'll see: 使用shell脚本从文本文件中获取特定的行

你将会看到:

#6


2  

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。也就是说,如果输入文件包含行延续,它们将被算作一行。您可以通过向read命令添加-r来更改此行为。(这可能是我们希望的行为。)

#7


0  

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.

还要注意优化,当我们得到我们要查找的线时,要跳出循环。

#8


0  

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

#9


0  

line=5; prep=`grep -ne ^ file.txt | grep -e ^$line:`; echo "${prep#$line:}"