首页 > 解决方案 > 如何读取 R 中以某个字符串开头的行

问题描述

假设我有一个文本文件,如下所示。我的问题是如何读取以SNR 开头的行。整个文件有超过 10k 行,但我只想要以 . 开头的行SN

# This file was produced by samtools stats (1.10+htslib-1.10.2) and can be plotted using plot-bamstats
# CHK, CRC32 of reads which passed filtering followed by addition (32bit overflow)
CHK     c643fc89        1179dda6        e11a337d
# Summary Numbers. Use `grep ^SN | cut -f 2-` to extract this part.
SN      raw total sequences:    1784681
SN      filtered sequences:     0
SN      sequences:      1784681
SN      is sorted:      1
SN      1st fragments:  894414
# First Fragment Qualities. Use `grep ^FFQ | cut -f 2-` to extract this part.
# Columns correspond to qualities and rows to cycles. First column is the cycle number.
FFQ     1       0       0       143     0       0       0       0       0       0       0       0       0       0       0       0       12403   0       0       0       0       0       0       0       0       0       0       0       13187   0       0       3879    42036   14537   37413   9464    761352  0       0       0       0       0       0
FFQ     2       0       0       4       0       0       0       0       0       0       0       0       0       0       0       0       11751   0       0       0       0       0       0       0       0       0       0       0       11151   0       0       5740    40065   18407   34914   14779   757603  0       0       0       0       0       0

标签: r

解决方案


使用readLines()然后grep是 SN 的。不过,首先阅读整个内容。

rl <- readLines('test.txt')
rl[grep('^SN.*', rl)]
# [1] "SN      raw total sequences:    1784681" "SN      filtered sequences:     0"      
# [3] "SN      sequences:      1784681"         "SN      is sorted:      1"              
# [5] "SN      1st fragments:  894414"        

推荐阅读