user3022917 user3022917 - 3 months ago 20
Bash Question

if grep value greater than 1000 then

I want to grep "priority=" in a file where the value of "priority=" is greater than 1000.

I tried something like this:

if grep -q "priority=[ >1000 ]" file; then
echo "[!] Unnatural priority"
fi


e.g.
<intent-filter android:priority="2147483647">

Answer

You could use this Perl one-liner:

perl -lne 'print "[!] Unnatural priority" if /priority="(\d+)"/ && $1 > 1000'

Capture the digits in priority="X" and print the warning if the value is greater than 1000.

You can also do this in native bash if you want:

while read -r line; do 
    if [[ $line =~ priority=\"([[:digit:]]+)\" ]] && (( BASH_REMATCH[1] > 1000 )); then
        echo "[!] Unnatural priority"
    fi
done < file
Comments