Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep using grep results

Tags:

bash

shell

Let's say I have two files containing attribute names and properties, where different data is stored in two files:

1.txt:

abc12      3@     50
edf13      4@     50
dde8       3@     50

2.txt

abc12      3@     65
edf13      4@     50
dde8       3@     70
dde7       3@     70

I want to grep as follows: first- grep "3@" 1.txt | awk '{print $1}', then grep * 2.txt where star(*) represent output of first grep.

all should be done in a single line from the prompt.

expected output:

abc12      3@     65
dde8       3@     70

Thanks

like image 557
Roi Berkovich Avatar asked Sep 23 '26 12:09

Roi Berkovich


1 Answers

With one awk process:

awk 'FNR==NR{if ($2 ~ "3@") a[$1]++; next} a[$1]' 1.txt 2.txt

the first condition{action} means when reading from first file, increase the array element with this first field as hash. The second condition a[$1] is true when this element is one or more, this is checked for the second file and the default action is to print the line.

like image 78
thanasisp Avatar answered Sep 26 '26 03:09

thanasisp



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!