DevyDev
DevyDev

Reputation: 886

Linux / Unix selecting specific data in same row

I've tried to do this by my self using grep, awk, search and many other commands i found on Google but it didn't worked out.

For example, i have test.txt that contains:

aaa, bbb, 333, 222,
ccc, kkk, 222, eee,
333, ooo, qqq, 555,
333, iii, uuu, 222,

and what i need to do, to select rows, that has 222 and 333 in a same row. So i should get :

aaa, bbb, 333, 222,
333, iii, uuu, 222,

What I tried so far is :

$ egrep -R -w '222|333' test.txt     
$ awk '/222|333/' failas

also I know how to find rows, which is not having 222 or 333

$ grep -v -e "222" -e "333"

But as i said, I still can't find command to find that specific data, in a same row.

Thanks for help.

Upvotes: 1

Views: 134

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174696

I'd use awk.

$ awk '/222/ && /333/' file
aaa, bbb, 333, 222,
333, iii, uuu, 222,

OR

$ grep -P '^(?=.*\b222\b)(?=.*\b333\b)' file
aaa, bbb, 333, 222,
333, iii, uuu, 222,

Upvotes: 3

Cyrus
Cyrus

Reputation: 88563

With GNU grep:

grep 222, file | grep 333,

or

grep '222,.*333,\|333,.*222,' file

or

grep -e '222,.*333,' -e '333,.*222,' file

Output:

aaa, bbb, 333, 222,
333, iii, uuu, 222,

Upvotes: 2

Related Questions