在unix中删除某些单词之后

时间:2016-03-14 09:46:39

标签: unix

Hi all,

    i have a text file containing details as shown below.i want to extract host and port from the text and paste it in other txt file
  1. (PROTOCOL = NIL)(HOST = 123.256.36.20)(PORT = 1256)
  2. (DESCRIPTION =(ADDRESS_LIST =(PROTOCOL = NIL) 的(HOST = 123.256.36.23)(PORT = 1256))
  3. (DESCRIPTION =(ADDRESS_LIST =(PROTOCOL = NIL) (HOST = COLIN1-IMP.COM)(PORT = 1566)(DESCRIPTION =(ADDRESS_LIST =(PROTOCOL = NIL)(HOST = COLIN2-IMP.COM)(PORT = 1586)< /强>
  4. (PROTOCOL = NIL)(HOST = 123.256.36.25)(PORT = 1526)
  5. 必填项:

      
        
    1. 123.256.36.20:1256
    2.   
    3. 123.256.36.23:1256
    4.   
    5. COLIN1-IMP.COM:1566
    6.   
    7. COLIN2-IMP.COM:1586
    8.   
    9. 123.256.36.25:1526
    10.   

    使用的代码:

    cat for2.txt | perl -ne 'print "$1=$2\n" if(/HOST=([\w.]*.'-'*[\w.]*.).*PORT=(\d+)/)' > in.txt
    sed 's|[()]||g' in.txt > in1.txt
    

    通过使用perl我得到了主机和端口值...使用sed我删除了'()'..

    获得的输出:

      
        
    1. 123.256.36.20 = 1256
    2.   
    3. 123.256.36.23 = 1256
    4.   
    5. COLIN1-IMP.COM = 1586
    6.   
    7. 123.256.36.25 = 1526
    8.   

    perl未获得与 COLIN1-IMP.COM

    相同的 COLIN2-IMP.COM

    帮帮我..谢谢你

1 个答案:

答案 0 :(得分:0)

我找到了我怀疑的答案,我想与大家分享。

cat sample1.txt | perl -nle 's{HOST=([^)]*).*?PORT=([^)]*)}{print "$1:$2"}ge' > sample2.txt