bash 输出特定行的大文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8166431/
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
Output specific line huge text file
提问by balsagoth
I have a sql dump with 300mb that gives me an error on specific line.
我有一个 300mb 的 sql 转储,它在特定行上给我一个错误。
But that line is in the middle of the file. What is the best approach?
但该行位于文件的中间。最好的方法是什么?
head -n middleLine dump.sql > output
?
head -n middleLine dump.sql > output
?
Or can i output only the line i need?
或者我可以只输出我需要的行吗?
回答by Basile Starynkevitch
You could use sed -n -e 123456p your.dump
to print line 123456
您可以sed -n -e 123456p your.dump
用来打印第 123456 行
回答by ata
If the file is long, consider using
如果文件很长,请考虑使用
sed -n 'X{p;q}' file
Where X is the line number. It will stop reading the file after reaching that line.
其中 X 是行号。到达该行后,它将停止读取文件。
回答by Patrick Bergner
If sed
is too slow for your taste you may also use
如果sed
你的口味太慢,你也可以使用
cat $THE_FILE | head -n $DESIRED_LINE | tail -n 1
回答by Toby
This can also be done with Perl:
这也可以用 Perl 来完成:
perl -wnl -e '$. == 4444444 and print and exit;' FILENAME.sql
4444444 being the line number you wish to print.
4444444 是您要打印的行号。
回答by potong
This might work for you:
这可能对你有用:
sed 'X!d;q' file
where X is the line number.
其中 X 是行号。
回答by Eugene Yarmash
You can use sed:
您可以使用 sed:
sed -n "x p" dump.sql
where x
is the line number.
x
行号在哪里。
回答by frankliuao
You can also try awk
like:
您也可以尝试awk
:
awk 'NR==YOUR_LINE_NO{print}' file_name
回答by ihatetoregister
If you know a phrase on that line I would use grep
. If the phrase is "errortext" use:
如果您知道该行上的一个短语,我会使用grep
. 如果短语是“errortext”,请使用:
$ cat dump.sql | grep "errortext"