首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用read.table将txt导入R时出错

使用read.table将txt导入R时出错
EN

Stack Overflow用户
提问于 2014-04-12 07:33:50
回答 2查看 872关注 0票数 0

我使用read.table在R中导入数据,代码如下:

代码语言:javascript
复制
txt=read.table("C:\\Work\\Network Monitoring\\Text mining syslog\\small set.txt", sep="\t", col.names=c("message", "device"))

在txt文件中,每个数据行如下所示:

代码语言:javascript
复制
%SPANTREE-SP-2-RECV_PVID_ERR: Received BPDU with inconsistent peer vlan id 1 on GigabitEthernet6/21 VLAN49. sn1-65ag-hot-137b

在第一元素是消息而第二元素是设备的情况下,这两个元素由制表符分隔。

我收到此错误消息: error in scan(file,what,nmax,sep,dec,quote,skip,nlines,na.strings,:line 15没有2个元素

但是,我使用edit pad pro检查正则表达式,并在第15行中看到message元素和device元素之间仅有一个选项卡。

有人能帮上忙吗??

非常感谢你的所有投入!

因此,我在read.table命令中添加了fill=TRUE,即

代码语言:javascript
复制
txt=read.table("C:\\Work\\Network Monitoring\\Text mining syslog\\03_28_2MB.txt",sep="\t",fill=TRUE, col.names=c("message","device"))

下面的数据是一个样本,共40行。执行read.table命令后,有三条数据线,读取正确,这三条线为下表中的第16、34、38行。(很抱歉,在这个帖子中加粗对我不起作用。)所以我找到了所有三行包含标签#的行。我检查了正则表达式,标签#周围没有制表符。你知道怎样才能避免这个问题吗?

真的很感谢!

示例数据:

代码语言:javascript
复制
%SPANTREE-SP-2-RECV_PVID_ERR: Received BPDU with inconsistent peer vlan id 1 on GigabitEthernet6/21 VLAN49. sn1-65ag-hot-137b
%USER-3-SYSTEM_MSG: bcm_usd_isr_switch_event_cb:431: slot_num 0, event 2, memory error type 0x1, mem addr 0x1e62, cause bit 0x10020a8  - bcm_usd    dm2-x34x-str-5-10
%USER-3-SYSTEM_MSG: bcm_usd_isr_switch_event_cb:431: slot_num 0, event 2, memory error type 0x1, mem addr 0x580b, cause bit 0x10020a8  - bcm_usd    dm2-x34x-str-5-10
hkb-96cbe-1a /kernel: %KERN-4: tcp_auth_ok: Packet from 207.46.32.161:65261 unexpectedly has MD5 digest hkb-96cbe-1a
hkb-96cbe-1a /kernel: %KERN-4: tcp_auth_ok: Packet from 207.46.32.160:61061 unexpectedly has MD5 digest hkb-96cbe-1a
ash-96cbe-1a fpc5 MIC(5/1) link 2 SFP syslog throttling: %PFE-5: enabling syslogs for receive power alarms and warnings. (0/0) ash-96cbe-1a
%USER-3-SYSTEM_MSG: bcm_usd_isr_switch_event_cb:431: slot_num 0, event 2, memory error type 0x1, mem addr 0x18bae, cause bit 0x10020a8  - bcm_usd   am2-x3sb-xcg-2-1b
/kernel: %KERN-4: tcp_auth_ok: Packet from 207.46.32.161:59634 unexpectedly has MD5 digest  bl2-64c-1a
lon04-96cbe-1a mib2d[1976]: %DAEMON-6-SNMP_TRAP_LINK_UP: ifIndex 678, ifAdminStatus up(1), ifOperStatus up(1), ifName xe-7/2/0  lon04-96cbe-1a
db3-96c-1b snmpd[1640]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 10.180.183.76 to 213.199.144.24 (b9hFAPAHa) db3-96c-1b
%USER-3-SYSTEM_MSG: bcm_usd_isr_switch_event_cb:431: slot_num 0, event 2, memory error type 0x1, mem addr 0x1e69e, cause bit 0x10020a8  - bcm_usd   bl2-x3hl-xcg-10-07
db3-96c-1a snmpd[1607]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 10.180.183.76 to 213.199.144.23 (b9hFAPAHa) db3-96c-1a
lon04-96cbe-1a mib2d[1976]: %DAEMON-6-SNMP_TRAP_LINK_UP: ifIndex 749, ifAdminStatus up(1), ifOperStatus up(1), ifName xe-7/2/0.0    lon04-96cbe-1a
db3-96c-1a snmpd[1607]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 10.180.183.76 to 213.199.144.23 (public)    db3-96c-1a
db3-96c-1b snmpd[1640]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 10.180.183.76 to 213.199.144.24 (public)    db3-96c-1b
lon04-96cbe-1a rpd[1946]: %DAEMON-6: EVENT <UpDown> index 263 <Up Broadcast Multicast> address #0 54.e0.32.74.8f.cb lon04-96cbe-1a
co2-96c-1a snmpd[1666]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 100.101.103.220 to 207.46.32.88 (public)    co2-96c-1a
lon04-96cbe-1a /kernel: %KERN-4-: port status changed   lon04-96cbe-1a
co2-96c-1a snmpd[1666]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 100.101.103.220 to 207.46.32.88 (b9hFAPAHa) co2-96c-1a
HK2-A75BL-SRCH-1 Rib: %BGP-3-NOTIFICATION: received from neighbor 100.96.155.171 (AS 65534) 6/5 (Cease/connection rejected) 0 bytes ???
BLUDCFX01SPINECC01 Rib: %BGP-3-NOTIFICATION: received from neighbor 10.60.132.3 (AS 65247) 2/2 (Open Message Error/bad AS number) 0 bytes bludcfx01spinecc01
was01-96cbe-1b chassisd[1482]: %DAEMON-3: New CRC errors found on xfchip 0 plane 2 subport 16 xfport 4 new_count 1 aggr_count 245862    was01-96cbe-1b
HK2-A75BL-SRCH-1 Rib: %BGP-3-NOTIFICATION: received from neighbor 100.96.155.171 (AS 65534) 6/5 (Cease/connection rejected) 0 bytes ???
fpc2 SYSLOG: %PFE-6: 25 messages lost, message queue overflowed. ams-96gmr-mms-7a
bl2-96c-1b snmpd[1621]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 10.55.4.25 to 207.46.32.99 (b9hFAPAHa)  bl2-96c-1b
bl2-96c-1a snmpd[1685]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 10.55.68.25 to 207.46.32.98 (public)    bl2-96c-1a
lon04-96cbe-1a /kernel: %KERN-4-: port status changed   lon04-96cbe-1a
bl2-96c-1a snmpd[1685]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 10.55.68.25 to 207.46.32.98 (b9hFAPAHa) bl2-96c-1a
bl2-96c-1b snmpd[1621]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 10.55.68.25 to 207.46.32.99 (b9hFAPAHa) bl2-96c-1b
%USER-3-SYSTEM_MSG: bcm_usd_isr_switch_event_cb:431: slot_num 0, event 2, memory error type 0x1, mem addr 0xe94, cause bit 0x1002173  - bcm_usd blu-x3hl-xcg-11-12
sin-f6f-edgcom-1a big3d[1956]: 01333001:3: SSLConnect: SSL error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol  sin-f6f-edgcom-1a
bl2-96c-1b snmpd[1621]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 10.55.68.25 to 207.46.32.99 (public)    bl2-96c-1b
10.4.143.113-1 SSHD[214436976]: sshd_control.c(274) 54449271 %% exited = 0  db3-lmb-hot-113-01a
lon04-96cbe-1a rpd[1946]: %DAEMON-6: EVENT <UpDown> index 263 <Broadcast Multicast> address #0 54.e0.32.74.8f.cb lon04-96cbe-1a
%USER-3-SYSTEM_MSG: bcm_usd_isr_switch_event_cb:431: slot_num 0, event 2, memory error type 0x1, mem addr 0xe14, cause bit 0x1002173  - bcm_usd blu-x3hl-xcg-11-12
ams-96c-1b snmpd[1616]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 10.183.235.204 to 213.199.144.30 (b9hFAPAHa)    ams-96c-1b
lon04-96cbe-1a mib2d[1976]: %DAEMON-4-SNMP_TRAP_LINK_DOWN: ifIndex 678, ifAdminStatus up(1), ifOperStatus down(2), ifName xe-7/2/0  lon04-96cbe-1a
lon04-96cbe-1a rpd[1946]: %DAEMON-6: EVENT <UpDown> xe-7/2/0.0 index 398 <Broadcast Multicast> address #0 54.e0.32.74.8f.cb lon04-96cbe-1a
bl2-96c-1a snmpd[1685]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 10.55.4.25 to 207.46.32.98 (public) bl2-96c-1a
bl2-96c-1b snmpd[1621]: %AUTH-5-SNMPD_AUTH_FAILURE: nsa_log_community: unauthorized SNMP community from 10.55.4.25 to 207.46.32.99 (public) bl2-96c-1b
EN

回答 2

Stack Overflow用户

发布于 2014-04-12 09:36:55

您可以考虑填充缺失的数据(fill=TRUE):

代码语言:javascript
复制
txt=read.table("C:\\Work\\Network Monitoring\\Text mining syslog\\small set.txt", sep="\t", fill=TRUE, col.names=c("message", "device"))

或者使用不同的分隔符(例如上的单词):

代码语言:javascript
复制
txt=read.table("C:\\Work\\Network Monitoring\\Text mining syslog\\small set.txt", sep="\son\s", col.names=c("message", "device"))
票数 0
EN

Stack Overflow用户

发布于 2014-04-12 11:33:19

您可以使用默认情况下为sep = "\t"read.delim

代码语言:javascript
复制
> txt <- "%SPANTREE-SP-2-RECV_PVID_ERR: Received BPDU with inconsistent peer
          vlan id 1 on GigabitEthernet6/21 VLAN49.  sn1-65ag-hot-137b"
> writeLines(txt, "smallSet.txt")
> read.delim("smallSet.txt", header = FALSE, col.names = c("message", "device"))                                                                                        
                                                                                                     message
1 %SPANTREE-SP-2-RECV_PVID_ERR: Received BPDU with inconsistent peer vlan id 1 on GigabitEthernet6/21 VLAN49
              device
1  sn1-65ag-hot-137b
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23024235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档