从bash脚本中的行号开始读取行

时间:2022-04-11 19:26:51

I'm trying to read a file line by line starting from a specific line in bash. I have already used the while command to read each line of the file by incrementing the count. Can I make it start from a specific line?

我正在尝试从bash中的特定行开始逐行读取文件。我已经使用while命令通过递增计数来读取文件的每一行。我可以从特定的线开始吗?

let count=0
declare -a ARRAY

while read LINE; do
ARRAY[$count]=$LINE 
vech=${ARRAY[$count]}
    if [...blah ..]
     then
    ...blah..
    fi 
sleep 2 
((count++)) 
done < filec.c 

Any kind of help in the form of suggestions or algorithms are welcome.

欢迎以建议或算法的形式提供任何形式的帮助。

Edit: I'm trying to pass the line number as a variable . I am Grepping for a specific pattern and if found, should pass the line number starting from the pattern.

编辑:我正在尝试将行号作为变量传递。我正在Grepping一个特定的模式,如果找到,应该从模式开始传递行号。

5 个解决方案

#1


12  

I would use sed's addresses to start at a particular line number and print to the end of the file:

我会使用sed的地址从特定的行号开始并打印到文件的末尾:

lineNumber=10
sed -n "$lineNumber"',$p' |
while read line; do
  # do stuff
done

Either that or, as Fredrik suggested, use awk:

或者,正如Fredrik建议的那样,使用awk:

lineNumber=10
awk "NR > $lineNumber" |
while read line; do
  # do stuff
done

#2


5  

What about something like this?

这样的事情怎么样?

while read -r line
do
    echo "$line"
done < <(tail -n +number file.name)

It's not POSIX compatible, but try on your Bash. Of course, do what you want with $line inside while loop.
PS: Change number with yhe number line you want and file.name with the file name.

它不是POSIX兼容的,但试试你的Bash。当然,在循环中用$ line做你想要的东西。 PS:使用您想要的数字行更改数字,使用文件名更改file.name。

#3


2  

Some of the many ways: http://mywiki.wooledge.org/BashFAQ/011

其中一些方法有:http://mywiki.wooledge.org/BashFAQ/011

Personally:

亲自:

printf '%s\n' {1..6} | { mapfile -ts 3 x; declare -p x; }                  

Also, don't use all-caps variable names.

另外,不要使用全大写变量名。

#4


1  

Just keep a counter. To print all lines after a certain line, you can do like this:

只要保持一个柜台。要在某一行之后打印所有行,您可以这样做:

#!/bin/bash

cnt=0
while read LINE
do
    if [ "$cnt" -gt 5 ];
    then
        echo $LINE
    fi
    cnt=$((cnt+1))
done < lines.txt

or, why not use awk:

或者,为什么不使用awk:

awk 'NR>5' lines.txt 

#5


0  

Just go a read a certain number of lines up to the number you want and start your logic to read the rest.

只需读取一定数量的行,直到你想要的数字,并开始你的逻辑阅读其余的。

There is no way to economize on a "text" file, you can't skip lines without actually reading them. The lines are delimited by 0x0a and of variable lengths. Therefore each delimiter must be scanned and counted to reach a certain "line-number". There are gimmicks that let you think you didn't read them, but you did.

没有办法节省“文本”文件,你不能跳过行而不实际读取它们。这些行由0x0a和可变长度分隔。因此,必须扫描每个分隔符并计数以达到某个“行号”。有噱头可以让你认为你没有读过它们,但是你做到了。

#1


12  

I would use sed's addresses to start at a particular line number and print to the end of the file:

我会使用sed的地址从特定的行号开始并打印到文件的末尾:

lineNumber=10
sed -n "$lineNumber"',$p' |
while read line; do
  # do stuff
done

Either that or, as Fredrik suggested, use awk:

或者,正如Fredrik建议的那样,使用awk:

lineNumber=10
awk "NR > $lineNumber" |
while read line; do
  # do stuff
done

#2


5  

What about something like this?

这样的事情怎么样?

while read -r line
do
    echo "$line"
done < <(tail -n +number file.name)

It's not POSIX compatible, but try on your Bash. Of course, do what you want with $line inside while loop.
PS: Change number with yhe number line you want and file.name with the file name.

它不是POSIX兼容的,但试试你的Bash。当然,在循环中用$ line做你想要的东西。 PS:使用您想要的数字行更改数字,使用文件名更改file.name。

#3


2  

Some of the many ways: http://mywiki.wooledge.org/BashFAQ/011

其中一些方法有:http://mywiki.wooledge.org/BashFAQ/011

Personally:

亲自:

printf '%s\n' {1..6} | { mapfile -ts 3 x; declare -p x; }                  

Also, don't use all-caps variable names.

另外,不要使用全大写变量名。

#4


1  

Just keep a counter. To print all lines after a certain line, you can do like this:

只要保持一个柜台。要在某一行之后打印所有行,您可以这样做:

#!/bin/bash

cnt=0
while read LINE
do
    if [ "$cnt" -gt 5 ];
    then
        echo $LINE
    fi
    cnt=$((cnt+1))
done < lines.txt

or, why not use awk:

或者,为什么不使用awk:

awk 'NR>5' lines.txt 

#5


0  

Just go a read a certain number of lines up to the number you want and start your logic to read the rest.

只需读取一定数量的行,直到你想要的数字,并开始你的逻辑阅读其余的。

There is no way to economize on a "text" file, you can't skip lines without actually reading them. The lines are delimited by 0x0a and of variable lengths. Therefore each delimiter must be scanned and counted to reach a certain "line-number". There are gimmicks that let you think you didn't read them, but you did.

没有办法节省“文本”文件,你不能跳过行而不实际读取它们。这些行由0x0a和可变长度分隔。因此,必须扫描每个分隔符并计数以达到某个“行号”。有噱头可以让你认为你没有读过它们,但是你做到了。