Skip to content
Advertisement

reading and analyzing a text file with bash script

I want to read a log file and want to extract 5-6 number digit that is written right next after the keyword “salary”. And then want to analyze if the salary is above 2000. If there is even one above 2000, it is a MNC otherwise unknown. After writing the salary, the line ends mostly but sometimes there is an email option.

My script currently looks like this.

salary=$(grep -o 'salary [1-9][0-9]+$' tso.txt | grep -o '[0-9]+')

echo $salary

if [ $salary >  2000 ]; then echo "it is mnc....."; else ":it is unknown....."; fi


Advertisement

Answer

This can be done in a simple awk like this:

awk '
{
   for (i=2; i<=NF; ++i)
      if ($(i-1) == "salary" && $i+0 > 2000) {
         mnc = 1
         exit
      }
}
END {
   print (mnc ? "it is mnc....." : "it is unknown.....")
}' file
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement