DarkSpark
DarkSpark

Reputation: 123

Using awk pattern to file filter data

I have the folling file(named /tmp/test99) which containd the rows:

"0","15","wall15"
123132,09808098,"0","15"

I am trying to filter the rows that contains "0" in the 3rd place, and "15" in 4th place (like in the second row) I tried running:

cat /tmp/test99 | awk '/"0","15"/{print>"/tmp/0_15_file.out"} '

but instead of getting only the second row, I get also the first row starting with "0","15".

Could you please help with the pattern ? Thanks:)

Upvotes: 1

Views: 71

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133428

Could you please try following.(comment on your effort, you need NOT to use cat with awk it could read Input_file by itself)

awk -F, '$3!~/\"0\"/ && $4!~/\"15\"/' Input_file

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

You may check if Fields 3 and 4 are equal to some hardcoded value using

awk -F, '$3=="\"0\"" && $4=="\"15\""'

Set the field separator to a comma and then, if Field 3 is "0" and Field 4 is "15" print the line, else discard.

See the online demo:

s='"0","15","wall15"
123132,09808098,"0","15"'
awk -F, '$3=="\"0\"" && $4=="\"15\""' <<< "$s" 
# => 123132,09808098,"0","15"

Upvotes: 2

Related Questions