Linux 如何使用 SED 获得仅第二行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7103863/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:44:20  来源:igfitidea点击:

How to get ONLY Second line with SED

linuxunixsed

提问by neversaint

How can I get second line in a file using SED

如何使用 SED 在文件中获取第二行

@SRR005108.1 :3:1:643:216
GATTTCTGGCCCGCCGCTCGATAATACAGTAATTCC
+
IIIIII/III*IIIIIIIIII+IIIII;IIAIII%>

With the data that looks like above I want only to get

有了上面的数据,我只想得到

 GATTTCTGGCCCGCCGCTCGATAATACAGTAATTCC

采纳答案by Jonathan

You don't really need Sed, but if the pourpose is to learn... you can use -n

你真的不需要 Sed,但如果要学习...你可以使用 -n

n read the next input line and starts processing the newline with the command rather than the first command

n 读取下一个输入行并开始使用命令而不是第一个命令处理换行符

sed -n 2p somefile.txt

Edit: You can also improve the performance using the tip that manatwork mentions in his comment:

编辑:您还可以使用 manatwork 在评论中提到的提示来提高性能:

sed -n '2{p;q}' somefile.txt

回答by Jacob

You always want the second line of a file? No need for SED:

你总是想要文件的第二行?不需要 SED:

head -2 file | tail -1

回答by Karoly Horvath

This will print the second line of every file:

这将打印每个文件的第二行:

awk 'FNR==2'

and this one only the second line of the first file:

而这只是第一个文件的第二行:

awk 'NR==2'

回答by potong

This might work for you:

这可能对你有用:

sed '2q;d' file

回答by Max Pan Ziyuan

cat your_file | head -2 | tail -1