如何使用 tr 删除八进制字符序列?

如何使用 tr 删除八进制字符序列?

我正在尝试从 .dat 文件中删除此: --More--这是该文件的一个片段,举个例子:

Device ID: N7K-LAN(JAF1651ANDL)
  IP address: 148.228.4.192
Interface: GigabitEthernet0/1,  Port ID (outgoing port): Ethernet7/19
Device ID: CIRC_INF_IDF1
  IP address: 148.228.107.252
Interface: FastEthernet0/20,  Port ID (outgoing port): GigabitEthernet0/1
  IP address: 148.228.107.252
Device ID: CIRC_INF_IDF3
  IP address: 148.228.107.250
 --More--         Interface: FastEthernet0/23,  Port ID (outgoing port): GigabitEthernet0/1
  IP address: 148.228.107.250
Device ID: CIRC_INF_IDF2
  IP address: 148.228.107.251
Interface: FastEthernet0/22,  Port ID (outgoing port): GigabitEthernet0/1
  IP address: 148.228.107.251
Device ID: SEP1CAA0711CFBE
 --More--           IP address: 148.228.199.103
Interface: FastEthernet0/2,  Port ID (outgoing port): Port 1
Device ID: SEP1CAA0711CD67
  IP address: 148.228.199.154
Interface: FastEthernet0/5,  Port ID (outgoing port): Port 1

使用十六进制文本编辑器我已经识别了不可见的字符 在此输入图像描述

现在我试图删除整个字符序列但我还没有成功,

这些是我的尝试:

tr -d --More--           < tabladetallada.dat >temporaltabla.dat

tr -d '\015' '\012' '\20' '\055' '\055' '\115' < tabladetallada.dat >temporaltabla.dat

sed 's/--More--␣//' tabladetallada.dat>tabladetallada2.dat

tr -d ' --More-- ^H^H^H^H^H^H^H^H^H        ^H^H^H^H^H^H^H^H^H' < tabladetallada.dat >temporaltabla.dat

有什么帮助吗?

谢谢。

这是它在 vi 中的样子:

Device ID: N7K-LAN(JAF1651ANDL)^M
  IP address: 148.228.4.192^M
Interface: GigabitEthernet0/1,  Port ID (outgoing port): Ethernet7/19^M
Device ID: CIRC_INF_IDF1^M
  IP address: 148.228.107.252^M
Interface: FastEthernet0/20,  Port ID (outgoing port): GigabitEthernet0/1^M
  IP address: 148.228.107.252^M
Device ID: CIRC_INF_IDF3^M
  IP address: 148.228.107.250^M
 --More-- ^H^H^H^H^H^H^H^H^H        ^H^H^H^H^H^H^H^H^HInterface: FastEthernet0/23,  Port ID (outgoing port): GigabitEthernet0/1^M
  IP address: 148.228.107.250^M
Device ID: CIRC_INF_IDF2^M
  IP address: 148.228.107.251^M
Interface: FastEthernet0/22,  Port ID (outgoing port): GigabitEthernet0/1^M
  IP address: 148.228.107.251^M
Device ID: SEP1CAA0711CFBE^M
 --More-- ^H^H^H^H^H^H^H^H^H        ^H^H^H^H^H^H^H^H^H  IP address: 148.228.199.103^M
Interface: FastEthernet0/2,  Port ID (outgoing port): Port 1^M
Device ID: SEP1CAA0711CD67^M
  IP address: 148.228.199.154^M
Interface: FastEthernet0/5,  Port ID (outgoing port): Port 1^M
Device ID: SEP1CAA0711D11A^M
  IP address: 148.228.199.4^M
Interface: FastEthernet0/10,  Port ID (outgoing port): Port 1^M
Device ID: SEPece1a985bb74^M
  IP address: 148.228.199.21^M
Interface: FastEthernet0/12,  Port ID (outgoing port): Port 1^M
Device ID: SEPf84f5794c5c4^M
  IP address: 148.228.199.23^M
Interface: FastEthernet0/11,  Port ID (outgoing port): Port 1^M
Device ID: SEP1CAA0711D276^M
  IP address: 148.228.199.102^M
 --More-- ^H^H^H^H^H^H^H^H^H        ^H^H^H^H^H^H^H^H^HInterface: FastEthernet0/9,  Port ID (outgoing port): Port 1^M
Device ID: SEPece1a985b5d5^M
  IP address: 148.228.199.24^M
Interface: FastEthernet0/14,  Port ID (outgoing port): Port 1^M
Device ID: SEP1CAA0711497C^M
Interface: FastEthernet0/4,  Port ID (outgoing port): Port 1^M
Device ID: SEPf84f5794c8c2^M
  IP address: 148.228.199.22^M
Interface: FastEthernet0/3,  Port ID (outgoing port): Port 1^M
Device ID: Circulo_Camaras4^M
 --More-- ^H^H^H^H^H^H^H^H^H        ^H^H^H^H^H^H^H^H^H  IP address: 148.228.101.5^M
Interface: FastEthernet0/24,  Port ID (outgoing port): GigabitEthernet1/0/24^M
  IP address: 148.228.101.5^M

答案1

sed -e 's/^[ ]*--More--.*\x8//' -e 's/\xD$//'

  • s/ */../我个人的偏好不是写,而是s/[ ]*/../因为当它是空格/制表符时,它会*明显地附加到左侧的原子上。
  • GNU sed具有六角匹配设施\x十六进制数字顺序。

答案2

最简单的解决方案就是换个角度思考。不要尝试定义您不需要的字符,而是定义您确实想要的字符并删除其他任何内容:

sed 's/--More--[^ a-zA-Z0-9]*//' file 

这将删除该字符串--More--及其后的 0 个或多个非空格、字母或数字的字符。根据您的数据,您可能需要稍微调整该设置(例如也允许_或您需要的任何内容)。

现在,您问题中的第一个--More--似乎包含退格字符(八进制 010、十六进制 7、ASCII \b),所以您也可以这样做:

perl -pe 's/[\b]//g' file

--More--或者,也删除:

perl -pe 's/--More--[\b]+//g' file

相关内容