×

Loading...
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!

关于匹配,请帮帮忙

我有一个文本文件,类似以下格式:
#----------
#
# department name
# contact: a b, a.b@company.ca, 416-xxx
# location: bc 103
# Netmask: 255.255.255.224
netname-a 111.111.111.128
#----------
#
# department name
# contact: c d, c.d@company.ca, 416-xxx
# location: bc 104
# Netmask: 255.255.255.0
netname-b 111.111.110.0

....

这个文件很长,我需要读出email, networkmask,netname, network 并转化
成以下格式文件;
netname-a;111.111.111.128:255.255.255.224;a.b@company.ca
netname-b;111.111.110.0:255.255.255.0;c.d@company.ca
...

请问有何方法?
多谢
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 关于匹配,请帮帮忙
    我有一个文本文件,类似以下格式:
    #----------
    #
    # department name
    # contact: a b, a.b@company.ca, 416-xxx
    # location: bc 103
    # Netmask: 255.255.255.224
    netname-a 111.111.111.128
    #----------
    #
    # department name
    # contact: c d, c.d@company.ca, 416-xxx
    # location: bc 104
    # Netmask: 255.255.255.0
    netname-b 111.111.110.0

    ....

    这个文件很长,我需要读出email, networkmask,netname, network 并转化
    成以下格式文件;
    netname-a;111.111.111.128:255.255.255.224;a.b@company.ca
    netname-b;111.111.110.0:255.255.255.0;c.d@company.ca
    ...

    请问有何方法?
    多谢
    • regular expression
      • Thanks, I know I can use regular expression, but don't know how to do
        • what language do you use?
          • under unix, can be ran within shell script or at command line. Thx!
    • Send me the file and $100, I will do it for you. Just kidding :D. Please see my scripts inside.
      If the file is exactly this way, you can use my script. Save the following scripts into awk.script. Suppose your file name is a.txt and you will generate b.txt. use this command: gawk -f awk.script <a.txt >b.txt
      If your UNIX doesn't have gawk, use command awk.
      It runs very fast. Isn't it worth $100? ;)


      BEGIN{EMAIL="";NETMASK="";}
      {if(NR%7==4){EMAIL=$5;sub(/\,/, "", EMAIL);}}
      {if(NR%7==6){NETMASK=$3;}}
      {if(NR%7==0){print $1 ";" $2 ":" NETMASK ";" EMAIL;EMAIL="";NETMASK="";}}
      • 笨笨,你很聪明阿。不过输出的结果有点不对,我正在研究,NR%7==4 是什么意思
        • 匹配不成功,由于以下原因:
          1。原文件格式不完全固定,比如说有些有“location"有些没有,所以如果按照7行来分段就有点乱了,
          2。第四行的第五个域不一定是email,
          举例如下,

          #---------------------------------------------------------------------------
          #
          # Generic Loopback Network
          # Contact: Network Engineering - xxxxx, net@company, 416-xxx
          # Netmask: 255.0.0.0
          loopback 127.0.0.0
          #---------------------------------------------------------------------------


          这段内容中既没有location一项(总共只有6行),而且由于contact中多了
          一些description,所以第5个域不是email地址

          笨笨,看你的啦
          • 别看我了. 如果不是一致的, 建议用C写程序吧.
        • 不要用聪明形容我. 我试过了的啊. 什么不对法? 是不是你的文件和贴出来的不完全一样? 是不是文件前面有其他行? NR%7==4 是当前行号对7取模结果应为4.
    • 用AWK吧。