Michael
Michael

Reputation: 51

awk command in csv: How do I print more than one entry?

I'm currently working on a spreadsheet project where I need to have it pumpout a copy with only specific entries in a column. Here's what I have to find one entry in column 3.

awk -F ',' -v OFS=',' '($3=6411){print}' testdata.csv > testdatacopy.csv

but if I wanted to find 6411, along with another entry, such as 6311, how would I do that?

I tried:

awk -F ',' -v OFS=',' '($3=6411,6311){print}' testdata.csv > testdatacopy.csv

but that did not work.

Upvotes: 1

Views: 102

Answers (3)

dlamblin
dlamblin

Reputation: 45331

Referring to the awk boolean operations, you want something like ($3==6411 || $3==6311).
Also please see the first "NOTE" in the book on sed and awk.

Make sure you notice that the relational operator "==" ("is equal to") is not the same as the assignment operator "=" ("equals").

If you're looking to match a good number of possible values, making the boolean expression cumbersome, you might try one of these approaches:

  1. Use a regular expression that matches any of the right values.
    awk -F ',' -v OFS=',' '($3~/^6(3[12357]1|399|411)$/){print}' …
    matches 6311 6321 6331 6351 6371 6399 or 6411
  2. Fill in an associative array with the values as the indexes allowing you to test for membership in the array:
    awk … 'BEGIN { '\
    'split("6311 6321 6331 6351 6371 6399 6411", v, " ");'\
    for(i in v){ a[v[i]] = 1;}\
    }($3 in a){print}' file_in.csv > file_out.csv
  3. If the values are luckily in a range you need only test for something like:
    awk … '($3>=6311 && $3<=6411)' …

Upvotes: 3

Michael Back
Michael Back

Reputation: 1871

Default action of a pattern match is to print:

awk -F, '$3==6411 || $3==6311' testdata.csv > testdatacopy.csv

Upvotes: 2

TessellatingHeckler
TessellatingHeckler

Reputation: 28993

Or you could do a regex "or" match with:

awk -F',' -v OFS=',' '($3 ~/6411|6311/){print}' t.csv

Edit: Ed Morton makes a good point - this would match "64112" and "86411" and so on, if the IDs go into longer numbers, unless you anchor the regex to lock the start and end of the field as well.

Upvotes: 0

Related Questions