Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GREP two particular columns

Tags:

linux

grep

bash

I have a file with two words on each row, using GREP i have to verify two conditions :

  1. if first word begins with a specific letter, for example 'A'

  2. same for second word but different letter, for example 'B'

i know how to do it verify it only for first word

grep '^A' file

But how to verify for second word ?

like image 584
Adrian Avatar asked Jun 11 '26 09:06

Adrian


1 Answers

You can use awk:

awk '$1 ~ /^A/ && $2 ~ /^B/' file

In case you want to avoid use of regex you can use this awk:

awk 'index($1, "A")==1 && index($2, "B")==1' file

Here is a egrep command for same but prefer awk:

egrep '^A[^[:blank:]]*[[:blank:]]+B' file
like image 66
anubhava Avatar answered Jun 12 '26 22:06

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!