代码之家  ›  专栏  ›  技术社区  ›  slayedbylucifer

awk:用字段分隔符连接匹配模式

  •  2
  • slayedbylucifer  · 技术社区  · 6 年前

    输入文件:

    cat /tmp/filename
    
    app_name="MysqlCluster"
    whatever whatever
    whatever whatever
    whatever whatever
    whatever whatever
    region="Europe"
    

    预期输出:

    MysqlCluster_Europe
    

    尝试:

    root@localhost:~# awk -F\" '/app_name|region/ {printf "%s_", $2}' /tmp/filename
    MysqlCluster_Europe_root@localhost:~# 
    
    root@localhost:~# awk -F\" '/app_name|region/ {OFS="_"; printf "%s", $2}' /tmp/filename
    MysqlClusterEuroperoot@localhost:~#
    
    root@localhost:~# awk -F\" '/app_name|region/ {printf "%s_", $2} END{print} ' /tmp/filename
    MysqlCluster_Europe_
    
    And few other attempts on similar lines but have not been successful.
    

    我在找:

    root@localhost:~# awk <here goes some awk magic> /tmp/filename
    MysqlCluster_Europe    <<< NOTE: No Underscore at the end
    
    5 回复  |  直到 6 年前
        1
  •  4
  •   oguz ismail FCulig    6 年前

    下面将为您的示例提供帮助。(但它不会打印换行。)

    awk -F\" '/^(app_name|region)/ { printf "%s%s", s, $2; s="_" }' /tmp/filename
    

    不要减少你的努力,除了处理 app_name region 在两个单独的操作中,IMO将更加实用,这样它也将支持多个应用程序名称-区域对。

    awk -F\" '/^app_name/ { printf "%s_", $2 } /^region/ { print $2 }' /tmp/filename
    
        2
  •  3
  •   James Brown    6 年前

    使用GNU AWK。即使文件中有更多的应用程序名称-区域对,也应该可以这样做:

    $ awk '
    /^(app_name|region)=/ {                     # look for matching records
        a[++c][1]                               # initialize 2d array 
        split($0,a[c],"\"")                     # split on quote
    }
    END {                                       # after processing file(s)
        for(i=1;i in a;i++)                     # loop all stored values
            printf "%s%s",a[i][2],(i%2?"_":ORS) # output
    }' file
    MysqlCluster_Europe
    
        3
  •  2
  •   Ivo Yordanov    6 年前

    这里有一个regex来匹配第1列中的app_名称和区域,然后由“=”字符分割。

    awk '$1 ~ "app_name|region" {split($0,a,"="); printf a[2]}' /tmp/filename | sed 's/"//g'
    

    Sed删除了双引号。

        4
  •  2
  •   Ed Morton    6 年前
    $ awk -F'=?"' '{f[$1]=$2} $1=="region"{print f["app_name"] "_" $2}' file
    MysqlCluster_Europe
    
        5
  •  0
  •   RavinderSingh13 Nikita Bakshi    6 年前

    你能试着跟一下吗?

    awk -F'"' '/app_name/{val=$(NF-1);next} /region/{print val "_" $(NF-1)}'  Input_file
    

    输出如下。

    MysqlCluster_Europe
    

    说明: 正在添加上述代码的解释。

    awk -F'"' '                 ##Setting field separator as " here for all lines.
    /app_name/{                 ##Checking condition if a line has string app_name then do following.
      val=$(NF-1)               ##Creating variable named val whose value is 2nd last field of current line.
      next                      ##Using next keyword will skip all further statements from here.
    }                           ##Closing block for this condition here.
    /region/{                   ##Checking condition if a line has string region then do following.
      print val "_" $(NF-1)     ##Printing variable val and OFS and $(NF-1) here.
      val=""                    ##Nullifying variable val here.
    }
    '  Input_file               ##Mentioning Input_file name here.