r - 如何读取 R 中以某个字符串开头的行
问题描述
假设我有一个文本文件,如下所示。我的问题是如何读取以SN
R 开头的行。整个文件有超过 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
解决方案
使用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"
推荐阅读
- linux - 将命令序列传递给正在运行的容器
- amazon-web-services - AWS lambda 只读文件系统错误,使用 docker 镜像存储 ML 模型
- python - 截屏并使用枕头将其转换为灰度图像的最佳方法是什么?
- gstreamer - 将 uridecodebin 动态添加到合成器
- java - 如何使用 Jsoup 从 HTML 中获取信息?
- qt - QPropetyAnimation 在 mosemove 事件中未按预期工作
- python - 在 Python 中一次只能单击一个按钮
- salesforce - 如何将 Amazon Alexa 与 Salesforce Einstein Bot 集成
- machine-learning - 如何理解多类逻辑回归的 ROC?
- node.js - 获取购物车 API 创建未添加产品的空购物车