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

时间:2014-04-11 23:33:50

标签: regex r import

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

txt=read.table("C:\\Work\\Network Monitoring\\Text mining syslog\\small set.txt", sep="\t", col.names=c("message", "device"))

在txt文件中,每个数据行都是:

%SPANTREE-SP-2-RECV_PVID_ERR: Received BPDU with inconsistent peer vlan id 1 on GigabitEthernet6/21 VLAN49. sn1-65ag-hot-137b

如果第一个元素是消息而第二个元素是设备,则这两个元素由制表符分隔。

我收到此错误消息: 扫描错误(文件,内容,nmax,sep,dec,quote,skip,nlines,na.strings,: 第15行没有2个元素

但是我使用编辑板专业版来检查正则表达式,并在消息元素和设备元素之间看到第15行中唯一的一个选项卡。

有人可以帮忙解决这个问题吗?

非常感谢你们所有的投入!

所以我在read.table命令中添加了fill = TRUE,这是

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行。 (对不起,在这个帖子中,粗体对我不起作用。)所以我发现所有三行都包含标签#。我检查了正则表达式,标签周围没有标签#。你知道我怎么能避免这个问题?

真的很感激!

示例数据:

%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

2 个答案:

答案 0 :(得分:0)

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

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

或使用不同的分隔符(例如一词):

txt=read.table("C:\\Work\\Network Monitoring\\Text mining syslog\\small set.txt", sep="\son\s", col.names=c("message", "device"))

答案 1 :(得分:0)

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

> 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