Village
Village

Reputation: 24433

Printing lines of a CSV containing 2 characters in 1 column and a pattern match in another column

I have a 4-column CSV file:

01, cat, animal, it catches mice

How can I print only those lines containing exactly 2 characters in column 2 while also matching the pattern "/to " anywhere on that line in column 4?

Upvotes: 0

Views: 181

Answers (3)

Gilles Quénot
Gilles Quénot

Reputation: 185560

You can use awk :

$ cat /tmp/l
01, cat, animal, it catches mice
02, ok, aaa, e/tomos
03, bad, qux, vb/tomos

$ awk -F"," 'length($2) == 3 && $4 ~ /\057to/' /tmp/l
02, ok, aaa, e/atmos

Upvotes: 1

potong
potong

Reputation: 58488

This might work for you:

sed '/^[^,]*,\s*..,[^,]*,.*\/to/!d' file

Upvotes: 1

PasteBT
PasteBT

Reputation: 2198

try this:

egrep "[^,]+,\s+[^,]{2},|([^,]+,\s+){3}.*/to.*" your_file

Try with this file:

01, cat, animal, it catches mice
01, ab, animal, it catches/o mice
01, ca, animal, it catches/to mice
01, cat, animal, it catches m/toice

and return:

01, ab, animal, it catches/o mice
01, ca, animal, it catches/to mice
01, cat, animal, it catches m/toice

Upvotes: 2

Related Questions