P. Leli
P. Leli

Reputation: 33

Awk filter csv by string start value

I want to filter out lines that START with word "CN" in the 2rd column from a file CSV and create a new CSV file with only that records.

Here is an example of my csv file

1;CN02BL00100000948999;;;
2;R30011781FUL1A60131PLCN1G34CN800166C0S00009C6UCB;;;
3;CN02BL00100000948569;;;
4;R 30011781FUL1A60131PLFG1G3BCC800625C0S00009C6UCB;;;

I'm using awk with this sintax

awk -F ";", '$1 ~ /CN/' $file > $TEMP/$file

But in this way my new file has row 1,2,3 and what i want is only row 1,3

I tryed to use ^ to filter from the start of the string in this way

awk -F ";", '$1 ~ /^CN/' $file > $TEMP/$file

but output file is null. What is wrong? Which is correct syntax?

Upvotes: 0

Views: 815

Answers (1)

jijinp
jijinp

Reputation: 2662

awk -F ";" '$2 ~ /^CN/' $file > $TEMP/$file

No need for , . and also you need to match for $2 not $1

Upvotes: 2

Related Questions