Box
Box

Reputation: 91

Counting valuesin a file that are greater than the argument given in Script [Edited & rewritten]

I was wondering if there was a way to use AWK or SED to find certain values in a certain file that are higher than the argument you gave to the script. This being

./script.sh CITY VALUE

The value, in this case, is the $2 or second argument which is going to the minimum value of our search, so if we have a file,

dummy2.txt

Format: Name;ID;Profession;email;rating;number of visits;balance

John Trevolta;12334;dentist;gmail;0;0;431
Isabella;4567;dentist;gmail;0;0;400
Maria;888;General Doctor;hotmail;4;2;700
Joseph;127;Intern;outlook;0;0;450

and if we input,

./script.sh Lisbon 400

(Ignore the Lisbon/CITY part as it doesn't pose a problem and it reads it from another file)

It should be able to read every column ( the seventh in this case ($7)) from this exact dummy2.txt and output the number of times it finds a value higher than the one we gave it so the desired output would be:

Found: 3

But the current Output I'm given is the following if I use the following expression:

awk -F';' '$7>=x' x="$2" medicos.txt | sort | uniq -c

1 John Travolta;12334;dentist;Gmail;0;0;431
1 Maria;888;General Doctor;hotmail;4;2;700
1 Joseph;127;Intern;outlook;0;0;450

OR

This is the output when with the current expression:

awk -v  x=$2 '$7>x{ i++ }END{ print "Found:" i }' dummy2.txt

Found:

So with this, this is my current code that I have for it:

#!/bin/bash
if [ "$#" -eq 0 ]
    then
        echo "Please insert arguments"
        exit 1
elif [ "$#" -lt 2 ] || [ -z "$1"  ]
    then
        echo "Error 404: No arguments found"
        exit 1
else
    grep -c "$1" dummy.txt
    awk -v  x=$2 '$7>x{ i++ }END{ print "Found:" i }' dummy2.txt
    fi

It doesn't start counting the i or even say it's value. I don't know if I need to initialize it or not and if I need to put ''/"/$ on it for it to be read.

Previous post which was a mess and everything was all over the place:

Counting the number of occurences or higher value in a file

Updated it but it remains closed.

Upvotes: 1

Views: 55

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133610

Could you please try following. Corrected/fixed the awk command here, rest of the bash conditions taken from OP's attempt itself.

Only awk command: which are looking for could be.

awk -v value="$2" 'BEGIN{FS=OFS=";"} $NF>=value{count++} END{print "Found: "count}'  Input_file

Your code could become like:

#!/bin/bash
if [ "$#" -eq 0 ]
    then
        echo "Please insert arguments"
        exit 1
elif [ "$#" -lt 2 ] || [ -z "$1"  ]
    then
        echo "Error 404: No arguments found"
        exit 1
else
     awk -v value="$2" 'BEGIN{FS=OFS=";"} $NF>=value{count++} END{print "Found: "count}'  Input_file
fi
.........rest of your things here.......

Explanation of above awk solution: Adding detailed explanation for above.

awk -v value="$2" '       ##Starting awk program from here, creating value variable here which has 2nd argument value passed to bash script here.
BEGIN{                    ##Starting BEGIN section of this program from here.
  FS=OFS=";"              ##Setting field separator and output field separator as ; here.
}
$NF>=value{               ##Checking condition if last field value is greater than or equal to value.
  count++                 ##Increasing value of count with 1 here.
}
END{                      ##Starting END block of this program from here.
  print "Found: "count    ##Printing Found: string with value of count here.
}
'  Input_file             ##Mentioning Input_file name here.

Issues in OP's attempt:

For attempt awk -v x=$2 '$7>x{ i++ }END{ print "Found:" i }': Logic is good with since there is NO field separator is set(it should be ; as per samples) hence its failing here.

For attempt awk -F';' '$7>=x' x="$2" medicos.txt | sort | uniq -c: Logic and awk looks good but as per question there is NO need for sort and uniq(not sure if later point of time it is needed for some other requirements of OP but for mentioned question it isn't needed).

Upvotes: 3

Related Questions