Bash script that analyzes report files

前端 未结 3 2008
伪装坚强ぢ
伪装坚强ぢ 2021-01-22 08:54

I have the following bash script which I will use to analyze all report files in the current directory:

#!/bin/bash    


# methods
analyzeStructuralErrors()
{ 
         


        
3条回答
  •  野的像风
    2021-01-22 09:10

    Bash has one-dimensional arrays that are indexed by integers. Bash 4 adds associative arrays. That's it for data structures. AWK has one dimensional associative arrays and fakes its way through two dimensional arrays. If you need some kind of data structure more advanced than that, you'll need to use Python, for example, or some other language.

    That said, here's a rough outline of how you might parse the data you've shown.

    #!/bin/bash    
    
    # methods
    analyzeStructuralErrors()
    { 
        local f=$1
        local Xpat="Error Code for Issue X"
        local notXpat="Error Code for Issue [^X]"
        while read -r line
        do
            if [[ $line =~ $Xpat ]]
            then
                flag=true
            elif [[ $line =~ $notXpat ]]
            then
                flag=false
            elif $flag && [[ $line =~ , ]]
            then
                # columns could be overwritten if there are more than one X section
                IFS=, read -ra columns <<< "$line"
            elif $flag && [[ $line =~ - ]]
            then
                issues+=(line)
            else
                echo "unrecognized data line"
                echo "$line"
            fi
        done
    
        for issue in ${issues[@]}
        do
            IFS=- read -ra array <<< "$line"
            # do something with ${array[0]}, ${array[1]}, etc.
            # or iterate
            for field in ${array[@]}
            do
                # do something with $field
            done
        done
    }
    
    # main
    find . -name "*_report*.txt" | while read -r f
    do
        echo "Processing $f"
        analyzeStructuralErrors "$f"
    done
    

提交回复
热议问题