查找不包含shell特定注释的XML文件

时间:2016-08-15 15:48:41

标签: xml bash awk grep

我想搜索(awk / grep / sed)到跳过某个文件夹的少量XML文件(pom.xml文件)。而且,第一个条件是它们必须包含标记 <!-- | Start of user code (user defined modules) |--> <!-- | End of user code |--> 。对于那些情况,我想打印出那些不包含下面确切序列的那些(它的自动生成的代码 - 它将帮助我检测是否有人修改了这个序列):

        fileArray=($(find . -type f -not -path "./folder1/*" -not -path "*/folder2/*" -not -path "./folder3/*" -name "pom.xml" \
                    | xargs awk -v RS='^$' 'match($0,/\<module>[^\n]+/,a){print a[0]}'))

我被困在这里:

  #!/bin/sh

###########################################################
# Checks for "user code" <modules> defined in pom files.
###########################################################

function check()
{
              # http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html

        OLDIFS=$IFS
        IFS=$'\n'

        # Read all pom files into an array
        # - Search for user code modules: It searches for the tag <module> into the pom files and in case they contain modules,
        #checks if the autogenerated section has been modified. Reading text secuence from foo.txt file
        #
        # - Exclude model folder as the codegen poms therein require such a repository



        fileArray=($(find . -type f -not -path "./folder1/*" -not -path "*/folder2/*" -not -path "./folder3/*" -name "pom.xml" \
                         | xargs `awk -v RS='^$' 'NR==FNR{str=$0;next} /<module>/ && !index($0,str){print FILENAME}' sequence {} +`))


        IFS=$OLDIFS

        # get length of an array
        numberOfFiles=${#fileArray[@]}

        # read all filenames
        for (( i=0; i<${numberOfFiles}; i++ ));
        do
          echo "ERROR:Found user code modules (file:line:occurrence): ${fileArray[$i]}"
        done


    if [ "$numberOfFiles" != "0" ]; then
        echo "SUMMARY:Found $numberOfFiles pom.xml file(s) containing user code modules."
        exit 1
    fi
}

check

请一些提示?

--- UPDATE:

    :~/temp> bash script.sh
awk: cmd. line:1: fatal: cannot open file `{}' for reading (No such file or directory)
ERROR:Found user code modules (file:line:occurrence): ./test_folder/test4/pom.xml ./tes                                                                        t_folder/test1/pom.xml ./test_folder/test2/pom.xml ./test_folder/test3/pom.xml
SUMMARY:Found 1 pom.xml file(s) containing user code modules.

---- UPDATE(最后一个控制台输出)

Speed = randi(99,3,1) % some arbitrary data
edge = linspace(1, 99, 50);
New = arrayfun(@(a) find(a>edges,1,'last'),Speed)

1 个答案:

答案 0 :(得分:0)

您可以使用xmllint使用xpath

搜索节点
xmllint --xpath '//module' */pom.xml

它的返回代码可以告诉您何时找到它(0)(!= 0)。