简体   繁体   English

使用带有xml值的变量进行awk模式匹配

[英]awk pattern matching using variable with xml value

so here's my awk script. 所以这是我的awk脚本。 It's in a file called mAwk.awk 在名为mAwk.awk的文件中

#!usr/bin/awk -f
  BEGIN {
    FS="."
     artifactPattern="/<artifactId>artifactName1|artifactName2<\\/artifactId>/"
 #   print "-------------" artifactPattern
  }
  {
    toPrint = 1
    if ($0 ~ /<dependencies>/) {
      matches=1000;
    }
    else if ($0 ~ /<dependency>/) {
      matches +=100;
    }
    else if ($0 ~ /<\/dependency>/) {
      matches =1000;
    }
  else if ($0 ~ /<groupId>(com.group1.*)|(com.group2.*)|(com.group3.*)<\/groupId>/) {
      matches += 10;
    }
# else if($0 ~ /<artifactId>artifactName1|artifactName2<\/artifactId>/){
 else if($0~artifactPattern){
        matches += 1;
        }
  else if ($0 ~ /<version>[0-9]+\.[0-9]+\.[0-9]+<\/version>/) {
     print "debugging: matched 1 -", matches
      if (matches == 1111) {
        lastPart="0-SNAPSHOT</version>"
        print $1 "." $2+1 "." lastPart;
        matches -= 11;
        toPrint = 0
      }
    }
    else if ($0 ~ /<\/dependencies>/) {
      matches=0
    }
    if ( toPrint == 1) {
      print $0
    }
  }
  END {
  }

Now here's the structure of the xml file (it's a pom.xml), just in-case: 现在,这是xml文件的结构(它是pom.xml),以防万一:

<project>
  <random tags/>

  <dependencies>
    <dependency>
      <groupId>data</groupId>
      <artifactId>data</artifactId>
      <version>1.2.3</version>
    </dependency>
      ... repeat...
  </dependencies>
</project

The problem is, if I use the line: 问题是,如果我使用该行:

# else if($0 ~ /<artifactId>payment-common|test2-common<\/artifactId>/){

instead of the one just below it, it matches just fine, but when I put the value inside a variable, it fails. 而不是它下面的那个,它匹配得很好,但是当我将值放在变量中时,它失败了。 What's going on here? 这里发生了什么?

My final aim is to call this through a shell script like... 我的最终目标是通过像...这样的shell脚本来调用它。

awk -v pattern=`cat ./artifactPatterns.txt` mAwk.awk -f myFile.xml

and the artifactPatterns.txt will look like waht the variable holds in the awk file, example: 而artifactPatterns.txt看起来就像变量在awk文件中保持的位置,例如:

/<artifactId>artifactName1|artifactName2<\\/artifactId>/

I've tried a bunch of things and nothing seems to work, thank you for your time! 我已经尝试了很多方法,但似乎没有任何效果,谢谢您的宝贵时间!

Take out the // delimiters around the value of artifactPattern . 取出//围绕artifactPattern值的定界符。 These are the syntax for regexp literals, they don't belong in strings. 这些是regexp文字的语法,它们不属于字符串。 The use of the ~ operator implies that it's a regular expression. 使用~运算符表示它是一个正则表达式。

And since / isn't a delimiter, you don't need to escape it inside the value. 并且由于/不是定界符,因此您无需在值内将其转义。

artifactPattern="<artifactId>artifactName1|artifactName2</artifactId>"

Also, $0 ~ /pattern/ can be simplified to just /pattern/ -- when a regexp literal appears by itself, it defaults to matching against the whole line. 同样, $0 ~ /pattern/可以简化为/pattern/ -当一个regexp文字本身出现时,它默认与整行匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM