bash 如何在bash中从第x行读取文件到文件末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14110223/
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
how to read file from line x to the end of a file in bash
提问by ylas
I would like know how I can read each line of a csv
file from the second line to the end of file in a bash script.
我想知道如何csv
在 bash 脚本中从第二行到文件末尾读取文件的每一行。
I know how to read a file in bash:
我知道如何在 bash 中读取文件:
while read line
do
echo -e "$line\n"
done < file.csv
But, I want to read the file starting from the second line to the end of the file. How can I achieve this?
但是,我想从第二行开始读取文件到文件末尾。我怎样才能做到这一点?
回答by Martin
tail -n +2 file.csv
From the man page:
从手册页:
-n, --lines=N
output the last N lines, instead of the last 10
...
If the first character of N (the number of bytes or lines) is a '+',
print beginning with the Nth item from the start of each file, other-
wise, print the last N items in the file.
In English this means that:
在英语中,这意味着:
tail -n 100
prints the last 100 lines
tail -n 100
打印最后 100 行
tail -n +100
prints all lines starting from line 100
tail -n +100
打印从第 100 行开始的所有行
回答by fge
Simple solution with sed
:
简单的解决方案sed
:
sed -n '2,$p' <thefile
where 2
is the number of line you wish to read from.
2
您希望从中读取的行数在哪里。
回答by F. Hauri
Or else (pure bash)...
否则(纯bash)...
{ for ((i=1;i--;));do read;done;while read line;do echo $line;done } < file.csv
Better written:
写得更好:
linesToSkip=1
{
for ((i=$linesToSkip;i--;)) ;do
read
done
while read line ;do
echo $line
done
} < file.csv
This work even if linesToSkip == 0 or linesToSkip > file.csv's number of lines
即使linesToSkip == 0 或linesToSkip > file.csv 的行数也能正常工作
Edit:
编辑:
Changed ()
for {}
as gniourf_gniourf enjoin me to consider: First syntax generate a sub-shell, whille {}
don't.
更改()
为{}
gniourf_gniourf 要求我考虑:第一种语法生成子 shell,而{}
不要。
of course, for skipping only one line (as original question's title), the loop for (i=1;i--;));do read;done
could be simply replaced by read
:
当然,为了只跳过一行(作为原始问题的标题),循环for (i=1;i--;));do read;done
可以简单地替换为read
:
{ read;while read line;do echo $line;done } < file.csv
回答by Rubens
There are many solutions to this. One of my favorite is:
对此有很多解决方案。我最喜欢的一个是:
(head -2 > /dev/null; whatever_you_want_to_do) < file.txt
You can also use tail
to skip the lines you want:
您还可以使用tail
跳过您想要的行:
tail -n +2 file.txt | whatever_you_want_to_do
回答by gniourf_gniourf
Depending on what you want to do with your lines: if you want to store each selected line in an array, the best choice is definitely the builtin mapfile
:
取决于你想对你的行做什么:如果你想将每个选定的行存储在一个数组中,最好的选择肯定是 builtin mapfile
:
numberoflinestoskip=1
mapfile -s $numberoflinestoskip -t linesarray < file
will store each line of file file
, starting from line 2, in the array linesarray
.
file
将从第 2 行开始,将 file 的每一行存储在数组中linesarray
。
help mapfile
for more info.
help mapfile
了解更多信息。
If you don't want to store each line in an array, well, there are other very good answers.
如果您不想将每一行存储在数组中,那么还有其他非常好的答案。
As F. Hauri suggests in a comment, this is only applicable if you need to store the whole file in memory.
正如 F. Hauri 在评论中建议的那样,这仅适用于需要将整个文件存储在内存中的情况。
Otherwise, you best bet is:
否则,你最好的选择是:
{
read; # Just a scratch read to get rid (pun!) of the first line
while read line; do
echo "$line"
done
} < file.csv
Notice: there's no subshell involved/needed.
注意:不涉及/需要子shell。
回答by Ivan Villareal
This will work
这将工作
i=1
while read line
do
test $i -eq 1 && ((i=i+1)) && continue
echo -e "$line\n"
done < file.csv
回答by traditional
I would just get a variable.
我只会得到一个变量。
#!/bin/bash
i=0
while read line
do
if [ $i != 0 ]; then
echo -e $line
fi
i=$i+1
done < "file.csv"
UPDATEAbove will check for the $i
variable on every line of csv. So if you have got very large csv file of millions of line it will eat significant amount of CPU cycles, no good for Mother nature.
上面的更新将检查$i
csv 每一行上的变量。因此,如果您有数百万行的非常大的 csv 文件,它将消耗大量的 CPU 周期,这对大自然没有好处。
Following one liner can be used to delete the very first line of CSV file using sed
and then output the remaining file to while
loop.
可以使用下面的一行来删除 CSV 文件的第一行,sed
然后将剩余的文件输出到while
循环中。
sed 1d file.csv | while read d; do echo $d; done