使用 Sed/AWK/Perl 从块中提取第 K 行
我有一些数据看起来像这样。它分为四行。每个块都以 @
字符开头。
@SRR037212.1 FC30L5TAA_102708:7:1:741:1355 length=27
AAAAAAAAAAAAAAAAAAAAAAAAAAA
+SRR037212.1 FC30L5TAA_102708:7:1:741:1355 length=27
::::::::::::::::::::::::;;8
@SRR037212.2 FC30L5TAA_102708:7:1:1045:1765 length=27
TATAACCAGAAAGTTACAAGTAAACAC
+SRR037212.2 FC30L5TAA_102708:7:1:1045:1765 length=27
88888888888888888888888888
我想做的是提取每个块的最后一行。产量:
::::::::::::::::::::::::;;8
888888888888888888888888888
请注意,块的最后一行可能包含任何标准 ASCII 字符 包括@
。
有没有一种有效的单行文字可以做到这一点?
I have some data that looks like this. It comes in chunk of four lines. Each chunk starts with a @
character.
@SRR037212.1 FC30L5TAA_102708:7:1:741:1355 length=27
AAAAAAAAAAAAAAAAAAAAAAAAAAA
+SRR037212.1 FC30L5TAA_102708:7:1:741:1355 length=27
::::::::::::::::::::::::;;8
@SRR037212.2 FC30L5TAA_102708:7:1:1045:1765 length=27
TATAACCAGAAAGTTACAAGTAAACAC
+SRR037212.2 FC30L5TAA_102708:7:1:1045:1765 length=27
88888888888888888888888888
What I want to do is to extract last line of each chunk. Yielding:
::::::::::::::::::::::::;;8
888888888888888888888888888
Note that the last line of the chunk may contain any standard ASCII character
including @
.
Is there an effective one-liner to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
以下 sed 命令将打印模式后的第三行:
The following sed command will print the 3rd line after the pattern:
如果没有空行:
If there are no blank lines:
如果你总是将这 4 行放在一个块中,那么其他一些方式
似乎你的行总是在以“+”开头的行之后,所以
If you always have those 4 lines in a chunk, some other ways
It also seems like your line is always after the line that start with "+", so
这会打印以
@
开头的之前行以及最后一行。它可以处理大小不统一的块,但假设只有块前导行以@
开头。一些解释是按顺序进行的:
1d
$p
x;d
x;p
This prints the lines before lines that starts with
@
, and also the last line. It can work with non uniform sized chunks, but assumes that only a chunk leading line starts with@
.Some explanation is in order:
1d
$p
x;d
x;p
这与dogbane的答案类似
,并且像那个答案一样,无论每个块中有多少行(只要至少有4行)都会起作用。
然而,与该答案的直接模拟是:
This works similarly to dogbane's answer
And, like that answer, will work regardless of the number of lines in each chunk (as long as there are at least 4).
The direct analog to that answer, however, would be:
这可以使用 grep 轻松完成
this can be done using grep easily
这可能对你有用(GNU sed):
This might work for you (GNU sed):