0

I am newbie to BASH scripting and was wondering if you could point me in the right direction. I have CSV file with the following field names separated by ","

no,name,score,city

I am trying to develop a script that will take 1 argument (in numeric format), search "score"column in csv file and extract entries greater than the given argument.

I am not allowed to use awk to achieve this, following is my draft code. I have tried different logic and approaches with cut and grep but don't get the desired results such as below. Is there any switch that I can use with grep to specify the field and apply condition ?

cut -f1,2,3,4 -d, sample.csv | grep 108

Additionally - I have prepared below script but having issues with the syntax and was wondering if anyone could help me or give me a hint. Thank you

EDITED

    #!/bin/bash
    file="sample.csv"
    arg="$1"
    while IFS=',' read -r f1 f2 f3 f4
    do
        if [ "$f3" -gt "$arg" ]
        then
             echo "$f1,$f2,$f3,$f4"
        else
            echo "No score greater than "$arg""
        fi
   done < $file
6
  • 2
    Please paste your script there first: shellcheck.net Commented Mar 1, 2020 at 6:35
  • 1
    Also read the documentation for test(1) after you fix the typo. Commented Mar 1, 2020 at 7:54
  • 1
    A general comment about CSV: Fields can contain linefeeds (new line characters) and commas. So processing a CSV with something like IFS=',' read f1 f2 f3 f4 only works if you can ensure, that no member contains a newline and/or a comma! A counterexample for a city with comma is the german city Wetter, Ruhr. Commented Mar 1, 2020 at 11:02
  • As bash always call other tools/programs, I can provide a bash/php solution, where the php call looks like php -r 'some commands;'. Commented Mar 1, 2020 at 11:03
  • @Cyrus thank you - the site is really helpful. I have edited my script, I do get the entries greater than the argument passed on with the script. But it is also displaying all entries less than the passed argument value with the echo message. I don't want to show these values and echo the message only when there is no value greater than the argument. for example when giving pass 30 - it displays "1,john,40,NY" and also echos " No score greater than 30" for all entries less than 30. Commented Mar 1, 2020 at 16:54

1 Answer 1

1

Try this:

#!/bin/bash
    file="sample.csv"
    arg="$1"
    empty=1
    while IFS=',' read -r f1 f2 f3 f4
    do
        if [ "$f3" -gt "$arg" ]
        then
             echo "$f1,$f2,$f3,$f4"
             empty=0
        fi
    done < $file
    if [ "$empty" -eq 1 ]
    then
            echo "No score greater than $arg"
    fi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.