我正在读取R中的文本文件,并希望替换每3次出现的'|' '\ n'这是我的代码和输入数据
**Input Data**
======================
'Monday, November 2, 2015|10:21:27|17:58:12|Tuesday, November 3, 2015|10:13:09|18:52:44|Wednesday, November 4, 2015|10:11:52|18:40:36|Thursday, November 5, 2015|10:31:42|18:16:57|Friday, November 6, 2015|10:13:13|--|Saturday, November 7, 2015|--|--|Sunday, November 8, 2015|--|--|Monday, November 9, 2015|--|--|Tuesday, November 10, 2015|10:03:20|18:07:52|Wednesday, November 11, 2015|09:40:20|18:42:20|Thursday, November 12, 2015|10:38:56|18:37:20|Friday, November 13, 2015|10:45:26|18:09:54|Saturday, November 14, 2015|--|--|Sunday, November 15, 2015|--|--|Monday, November 16, 2015|--|--|Tuesday, November 17, 2015|10:11:43|18:36:15|Wednesday, November 18, 2015|--|--|Thursday, November 19, 2015|--|--|Friday, November 20, 2015|12:14:25|20:25:08|Saturday, November 21, 2015|--|--|Sunday, November 22, 2015|--|--|Monday, November 23, 2015|10:08:08|17:57:35|Tuesday, November 24, 2015|14:30:32|--|'
**My R-Code**
====================
emp <- readChar(FileDir, (file.info(FileDir)$size-172))
emp <- gsub("\r\n","|",emp)
empTMP <- gsub('([^|]*|[^|]*|[^|]*)|',"\1\n",emp)
**output**
====================
"\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n|\001\n"
**Required output**
====================
Monday, November 2, 2015|10:21:27|17:58:12
Tuesday, November 3, 2015|10:13:09|18:52:44
Wednesday, November 4, 2015|10:11:52|18:40:36
Thursday, November 5, 2015|10:31:42|18:16:57
Friday, November 6, 2015|10:13:13|--
Saturday, November 7, 2015|--|--
Sunday, November 8, 2015|--|--
Monday, November 9, 2015|--|--
Tuesday, November 10, 2015|10:03:20|18:07:52
Wednesday, November 11, 2015|09:40:20|18:42:20
Thursday, November 12, 2015|10:38:56|18:37:20
Friday, November 13, 2015|10:45:26|18:09:54
Saturday, November 14, 2015|--|--
Sunday, November 15, 2015|--|--
Monday, November 16, 2015|--|--
Tuesday, November 17, 2015|10:11:43|18:36:15
Wednesday, November 18, 2015|--|--
Thursday, November 19, 2015|--|--
Friday, November 20, 2015|12:14:25|20:25:08
Saturday, November 21, 2015|--|--
Sunday, November 22, 2015|--|--
Monday, November 23, 2015|10:08:08|17:57:35
Tuesday, November 24, 2015|14:30:32|--
请帮助我做错了,我在文本编辑器中检查了上面的正则表达式,它工作正常,但是在“ R”中,它不能产生正确的结果。
最佳答案
empTMP <- gsub('([^|]*|[^|]*|[^|]*)|',"\1\n",emp)
这是引起恕我直言的麻烦的路线。
它应该是
empTMP <- gsub('([^|]*|[^|]*|[^|]*)|',"\\1\n",emp)
(请注意
\\1
)侧面:为什么要为此任务使用R。看起来像是标准Shell脚本的东西。
方面2:为什么要使用Teradata?您在哪个TD盒上使用R?
关于regex - R中第n次出现的正则表达式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34571435/