selecting record by matching in two columns values

Hi Guys !

i want to search a record in file by matching two values in a record in two different columns

suppose i have 3 columns

and i want to select all those values from col1 for which in col3 has a specific value

e.g select all "john" from column1 where column 3 has a value of "20"

using grep and cut command

thanks

TJ

Or awk?

awk '$1=="john" && $3==20'

Hi Scrutinizer !

thanks for your help but is it not possible without awk? as i mentioned with grep and cut commands?

BR

TJ

Hi, you could make a concoction with cut and grep but I don't think that would be very efficient. You can also do something with grep alone, but that would not be as accurate, e.g.:

grep 'john .* 20' 

(if the field separator is a space)
Is there a specific reason why you can't use awk?

Hi,

From the original post, I understand that he wants only the first field, although I'm not sure, so could be wrong. According with Scrutinizer, awk is one of the best tools for this job.

Here my approach:

$ cut -f1,3 infile | grep -E "john\s+20" | cut -f1

Regards,
Birei

Hi Scrutinizer !

i found the solution just wanted to share with you i used the back tracking technique

i first selected all the value = 20 from column 3 and then applied the cut on the first column like that...

but can you help me with what 's/ *\(.*\) \(.*\)/\2 \1/' is doing extactly?

grep '" 20 ' filename | cut -f1 -d' '| sort | uniq -c | sort -nr | sed 's/ *\(.*\) \(.*\)/\2  \1/' | column -t

BR,

TJ

---------- Post updated at 11:20 AM ---------- Previous update was at 11:11 AM ----------

Hi Scrutinizer

actually i'm doing something with the values i'm getting i know awk is more flexible but in my project i have not used awk and i not good at awk so that's why from futher difficulties i don't use awk for the time being

anyways thanks for your help and time

and one last question can we use column number in the grep command? as we use it in cut -f1? just wondering what will be the structure

Hi, sed 's/ *\(.*\) \(.*\)/\2 \1/' flips the last field and any previous fields around...
Grep does not know fields, so you cannot use something like f1 in cut or $1 in awk...

1 Like

Hi Scrutinizer !

thanks again you're so kind, i got your point and explaination you're great teacher

Best Wishes

T.J

Thanks, you're welcome! Have Fun..
S.

post a message in a wrong place sorry guys