Reputation: 51
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
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:
awk -F ',' -v OFS=',' '($3~/^6(3[12357]1|399|411)$/){print}' …
6311
6321
6331
6351
6371
6399
or 6411
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
awk … '($3>=6311 && $3<=6411)' …
Upvotes: 3
Reputation: 1871
Default action of a pattern match is to print:
awk -F, '$3==6411 || $3==6311' testdata.csv > testdatacopy.csv
Upvotes: 2
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