Home > Back-end >  Bash script read the user record from the CSV file
Bash script read the user record from the CSV file

Time:08-31

I have user.csv file which contains user record.

Users  marks 

A.      90
B.      70
C.      60
D.      40
E.      35

Now based on users Name, let say i have to save the user Name who ever taken the marks below 50 into new output.txt file.

Can you please some one assist how we can achieve this.

CodePudding user response:

Sample script:

#!/bin/bash
if [[ ! -f user.csv ]]
then
        echo "ls user.csv"
        ls user.csv
else
        for users in $(awk '{
                if ( 50 > $2 )
                {
                        printf( "%sSPACE%s\n", $1, $2);
                }
        }' user.csv)
        do
                USERNAME=$(echo $users | sed "s/SPACE.*//;")
                MARK=$(echo $users | sed "s/.*SPACE//;")
                grep "$USERNAME" output.txt >/dev/null 2>&1
                if [ 0 -ne $? ]
                then
                        echo "$USERNAME      $MARK" >>output.txt
                fi
        done
        echo cat output.txt
        cat output.txt
fi

Sample output:

$ cat "user.csv"
Users      Marks
A.      90
B.      70
C.      60
D.      40
E.      35
$ ./73541857.sh
cat output.txt
D.      40
E.      35
$ ls -lid output.txt
101049516639342374 -rw----r--  1 murugesan openssl 24 Aug 30 17:41 output.txt
$ ./73541857.sh
cat output.txt
D.      40
E.      35
$ ls -lid output.txt
101049516639342374 -rw----r--  1 murugesan openssl 24 Aug 30 17:41 output.txt

CodePudding user response:

awk '$NF < 50' user.csv > output.txt
  •  Tags:  
  • bash
  • Related