-2

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

./script.sh CITY VALUE

The value, in this case, is the $2 or second argument which is going to the basis 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

and if we input,

./script.sh CITY 400

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:

1

But the current Output I'm given is the following:

1 John Trevolta;12334;dentist;gmail;0;0;431

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

I have already added some suggestions found in the comments but unfortunately, it only prints,

Found:

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.

Box
  • 91
  • 6
  • `"$7 -gt $Arg2"` isn't a valid awk expression... – Shawn Oct 24 '20 at 00:37
  • Thanks for the heads-up! What do you think I should put in there tho? – Box Oct 24 '20 at 01:20
  • 1
    Something like `awk -F; '$7>x' x="$var1" dummy2.txt ...` – dawg Oct 24 '20 at 01:37
  • Edit: nevermind, I was missing a ". tho is it possible to instead of giving me the whole line, to just count how many? Instead of printing the whole line, say 2 if it found 2 greater than the agr – Box Oct 24 '20 at 02:16
  • oh and if you could explain to me in greater detail the way you used and why it gets recognized while mine doesn't. I'd appreciate it! I'm new to shell and I'm still learning it's quirks – Box Oct 24 '20 at 02:40
  • 1
    Calling `awk` from a shell script is rarely necessary. If you are using `awk` -- it can generally handle the entire program. – David C. Rankin Oct 24 '20 at 04:35
  • 1
    `if [awk -F';' "$7 -gt $Arg2"]` contains so many false assumptions and errors it's mind-boggling. The argument to `[` should be a single string and it will check whether that string is empty; you absolutely need a space between `[` and its argument; but obviously the string `awk` etc is not empty. `if awk -F';' -v value="$Arg2" '$7 <= value { exit 1 } END { exit 0 }'` makes sense syntactically, but it's not clear which file Awk should read as its standard input. – tripleee Oct 24 '20 at 11:10
  • Possible duplicate of https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script – tripleee Oct 24 '20 at 11:13
  • Being honest here, that if statement was written when I made the post, so I really didn't pay attention to the rules ( I know I probably should have ) but it was meant as an illustration of what I was thinking on doing in the if statement. hence me asking how to use it in an if statement. Do apologize for all the sore eyes after reading that bit of code! – Box Oct 24 '20 at 11:49

1 Answers1

1

Because of (the lack of ) input, assume the following input (numbers.txt):

een 1
twee 2
drie 3
vier 4
vijf 5
zes 6
zeven 7

Then:

awk -v arg1=2 '$2>arg1{ i++ }END{ print "Found " i " lines greater than " arg1 }' numbers.txt

will print: Found 5 lines greater than 2

and:

awk -v arg1=5 '$2>arg1{ i++ }END{ print "Found " i " lines greater than " arg1 }' numbers.txt

will print: Found 2 lines greater than 5

EDIT: (because....)

When creating a script like this (lets name it test.sh):

#!/bin/bash

rm numbers.txt
echo een 1 >>numbers.txt
echo twee 2 >>numbers.txt
echo drie 3 >>numbers.txt
echo vier 4 >>numbers.txt
echo vijf 5 >>numbers.txt
echo zes 6 >>numbers.txt
echo zeven 7 >>numbers.txt
echo acht 8 >>numbers.txt

awk -v arg1=3 '$2>arg1{ i++ }END{ print "Found " i " lines greater than " arg1 }' numbers.txt

awk -v arg1=5 '$2>arg1{ i++ }END{ print "Found " i " lines greater than " arg1 }' numbers.txt

awk -v arg1=$1 '$2>arg1{ i++ }END{ print "Found " i " lines greater than " arg1 }' numbers.txt

The output will be:

$ ./test.sh 7
Found 5 lines greater than 3
Found 3 lines greater than 5
Found 1 lines greater than 7
Luuk
  • 12,245
  • 5
  • 22
  • 33