0

I have a csv file that simplified for here looks something like this:

Brett, Spencer, 3, 11, inactive
Bendar, Scott, 4, 8, active
Singh, Jit, 11, 2, active

I need to do some kind of grep search (or any other widely available linux utility) that will output all lines in certain columns that add up to a certain value. So if I searched for lines where column 3 and 4 total 13 I would get:

Singh, Jit, 11, 2, active

1 Answer 1

4

This is a job for awk:

awk -F, '$3 + $4 == 13 { print }' /path/to/file
4
  • Anyway to get it to print out the filenames also? I'm using * for /path/to/file and searching through hundreds of files. { print FILENAME } gets me just the filename but I would like both the output of the filename and the line.
    – DavidR
    Commented Nov 6, 2017 at 23:41
  • Add another clause: FNR==1 { print FILENAME }
    – DopeGhoti
    Commented Nov 6, 2017 at 23:45
  • Or, to make it more like grep, do $3 + $4 == 13 { printf "%s: ", FILENAME; print }. Commented Nov 7, 2017 at 0:17
  • Why two print statements rather than print FILENAME " " $0? My suggestion will give you a 'header' block as each file is parsed; @G-Man's (or my reply to it) will prefix the file name to each line.
    – DopeGhoti
    Commented Nov 7, 2017 at 15:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .