-1

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
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    Change FROM `awk '$esal > 5000 { print $esal }' data.csv` **TO** `awk -F',' '$3 > 5000 { print $3 }' data.csv` – RavinderSingh13 Sep 08 '22 at 12:21
  • **OR** if you want to search first field number from your headers(1st line), which can be any field and you DO NOT want to hardcode. Then try following code: `awk 'BEGIN{FS=OFS=","} FNR==1{for(1;i<=NF;i++){if(i=="esal"){field=i;next}} $field>5000 {print $field}' Input_file` – RavinderSingh13 Sep 08 '22 at 12:24
  • nothing printed for this – P. BHAVANA Sep 08 '22 at 12:25
  • Try my 1st(updated) and 2nd code also now and let me know how it goes. – RavinderSingh13 Sep 08 '22 at 12:26
  • 2
    If Ravinders suggestion doesn't work for you then you probably have DOS line endings, see https://stackoverflow.com/questions/45772525/why-does-my-tool-output-overwrite-itself-and-how-do-i-fix-it. – Ed Morton Sep 08 '22 at 12:28

2 Answers2

1

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
dawg
  • 98,345
  • 23
  • 131
  • 206
0
$ awk -F, 'NR==1 || $NF>5000' file
karakfa
  • 66,216
  • 7
  • 41
  • 56