如何使用R从文本文件中提取包含特定单词或字符的句子

时间:2018-06-15 15:27:37

标签: r

我是R的新手,我遇到了一些问题。

我有一个包含章节号的文本文件,例如:

1.0
1.1
1.1.1
1.2
etc.

我有另一个文本文件,其句子在句子的开头包含这些数字,例如:

1.0       General
Random sentence.
1.1       Description
Random sentence.
1.1.1     Background
Random sentence.

我只想提取包含节号的行,所以基本上是:

1.0 General
1.1 Description
1.1.1 Background

1 个答案:

答案 0 :(得分:3)

在使用grep

阅读文件后,我们可以使用readLines检查第二个文字是否以数字开头
grep("^[0-9.]+", txt2, value = TRUE)

如果还有其他数字是句子的开头,那么也要读取第一个文件,并在提取子字符串后使用grep%in%

out <- txt2[sub("\\s+.*", "", txt2) %in% txt1]
cat(out, sep="\n")
#1.0       General
#1.1       Description
#1.1.1     Background

数据

txt1 <- readLines("file1.txt")
txt2 <- readLines("file2.txt")