简体   繁体   中英

Sort output in bash

I have the following script to check the parent versions of my different Maven projects by looking into their pom.xml.

#!/bin/bash
# Loop to find all pom.xml
for i in `find . ! -path "*/target/*" -type f -name pom.xml`
do
    # Filter the parent artifactId.
    grep -q 'IC_Maven_JEE_Parent\|IC_Maven_Parent' $i
    if [ "$?" -eq 0 ]; then
    # Print parent ID, version value, and then pom.xml path.
    echo $(grep -o 'IC_Maven_JEE_Parent\|IC_Maven_Paren' $i) $(echo -e 'setns x=http://maven.apache.org/POM/4.0.0\ncat /x:project/x:parent/x:version/text()' | xmllint --shell $i | grep -v /) $i
    fi
done

The output in my test folder looks like the following, which is unsorted and non-informative.

IC_Maven_JEE_Parent ------- 1.4.44 ./AGS-DEF-JOB1/pom.xml
IC_Maven_JEE_Parent ------- 1.4.44 ./AGS-SITE-JOB1/pom.xml
IC_Maven_Paren ------- 1.5.44 ./AGS-SITESSL-JOB1/pom.xml
IC_Maven_JEE_Parent ------- 6.2 ./AIRR-COMMODITIES-JOB1/pom.xml
IC_Maven_JEE_Parent ------- 6.2 ./AIRR-DEF14-JOB1/pom.xml
IC_Maven_Paren ------- 1.4.38 ./pom.xml

I want to have the output sorted (firstly according to parent Id, then version value, lastly path), so that I will know the priority of upgrading. It should look like the following.

IC_Maven_JEE_Parent ------- 1.4.44 ./AGS-DEF-JOB1/pom.xml
IC_Maven_JEE_Parent ------- 1.4.44 ./AGS-SITE-JOB1/pom.xml
IC_Maven_JEE_Parent ------- 6.2 ./AIRR-COMMODITIES-JOB1/pom.xml
IC_Maven_JEE_Parent ------- 6.2 ./AIRR-DEF14-JOB1/pom.xml
IC_Maven_Paren ------- 1.4.38 ./pom.xml
IC_Maven_Paren ------- 1.5.44 ./AGS-SITESSL-JOB1/pom.xml

An update to my post (June 08, 14:13).

The closest point I have achieved is by saving the output into a file then sort the file, as the following script shows:

#!/bin/bash
for i in `find . ! -path "*/target/*" -type f -name pom.xml`
do
    grep -q 'IC_Maven_JEE_Parent\|IC_Maven_Parent' $i
    if [ "$?" -eq 0 ]; then
    echo $(grep -o 'IC_Maven_JEE_Parent\|IC_Maven_Paren' $i) $(echo -e 'setns x=http://maven.apache.org/POM/4.0.0\ncat /x:project/x:parent/x:version/text()' | xmllint --shell $i | grep -v /) $i
    fi
done > check.txt
sort ./check.txt

You can simply add sort to end of your loop to sort the outputs.

#!/bin/bash
for i in `find . ! -path "*/target/*" -type f -name pom.xml`
do
    grep -q 'IC_Maven_JEE_Parent\|IC_Maven_Parent' $i
    if [ "$?" -eq 0 ]; then
    echo $(grep -o 'IC_Maven_JEE_Parent\|IC_Maven_Paren' $i) $(echo -e 'setns x=http://maven.apache.org/POM/4.0.0\ncat /x:project/x:parent/x:version/text()' | xmllint --shell $i | grep -v /) $i
    fi
done | sort

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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