Linux 从 bash 脚本中的行号开始读取行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11102360/
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
Read lines starting from a line number in a bash script
提问by Gil
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.
编辑:我试图将行号作为变量传递。我正在寻找特定模式,如果找到,应该传递从模式开始的行号。
采纳答案by Tim Pote
I would use sed
's addressesto 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
回答by ormaaj
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.
另外,不要使用全大写的变量名。
回答by Fredrik Pihl
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
回答by Simone-Cu
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。当然,在 while 循环中用 $line 做你想做的事。
PS:用你想要的数字线更改数字,用文件名更改文件名。
回答by pizza
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 分隔并且长度可变。因此,必须扫描和计算每个分隔符以达到某个“行号”。有些噱头让你以为你没有读过它们,但你确实读过。