Write a Shell Script to read the data from data.csv file and print the records into a new file having salary greater than 5000

Viewed 39

data.csv is the input file

eid,ename,esal
101,Raju,8000
106,Sanjay,5000
109,Anjali,4000

awk '$esal > 5000 { print $esal }' data.csv

the above is the command which i have tried. the output is:

eid,ename,esal
567,pinky,4000

Expected output is:

eid,ename,esal
101,Raju,8000
2 Answers

You can do something like this if you want to address columns by name:

awk  'BEGIN{FS=OFS=","}
    FNR==1{ 
    for(i=1;i<=NF;i++) {
        header[$i]=i
        printf("%s%s", $i, i==NF ? ORS : OFS)
        }
    next
    }
    $header["esal"]>5000
' file

Prints:

eid,ename,esal
101,Raju,8000
$ awk -F, 'NR==1 || $NF>5000' file
Related