bash:从一行中删除特定的字符模式

时间:2016-08-12 04:57:39

标签: bash awk sed grep tr

a.txt 文件的内容为:

10.39.105.16tcp(1234)   ABCDEF02  
10.49.105.13tcp(521)    ABCDEFV01  
10.19.105.12tcp(1241)   ABCDEFV01  
10.29.105.20tcp(1222)   ABCDEF03  
10.39.104.9tcp(131)     ABCDEF21  
10.49.104.34tcp(1512)   ABCDEF22  
10.89.104.120tcp(1384)  ABCDEF32     

如何实现以下输出:

10.39.105.10 ABCDEF02   
10.49.105.13 ABCDEFV01   
10.19.105.13 ABCDEFV01   
10.29.105.20 ABCDEF03   
10.39.104.1 ABCDEF21   
10.49.104.2 ABCDEF22   
10.89.104.130 ABCDEF32  

如何在上述文件的每一行中删除 tcp(*)模式?

2 个答案:

答案 0 :(得分:1)

您可以使用sed:

sed 's/tcp([0-9]\+)\s\+/ /' a.txt

这也将在tcp(*)之后摆脱尾随空格,并将其替换为输出中出现的单个空格。

要更改实际文件(而不仅仅是输出),请为inplace:

添加-i选项
sed -i 's/tcp([0-9]\+)\s\+/ /' a.txt

答案 1 :(得分:1)

根据您的目标,这可能就是答案:

    awk '{sub(/tcp(.+)/,"",$1)}1' file

    10.39.105.16 ABCDEF02
    10.49.105.13 ABCDEFV01
    10.19.105.12 ABCDEFV01
    10.29.105.20 ABCDEF03
    10.39.104.9 ABCDEF21
    10.49.104.34 ABCDEF22
    10.89.104.120 ABCDEF32