owwow14
1
Hi all,
I have two files.
File A looks like this:
abkhasian com|hum
accent com|com|com|com|sta
acceptation act|com|sta
adventures com|hum
adversity com|hum|hum
and File B looks like this:
adventure
adventures
adversary
Adverse
adversity
I want to print those lines in File A in which the value in Column 1 also appears in File B.
Thus, the desired output would be to print those lines from File A:
adventures com|hum
adversity com|hum|hum
I know there is a grep that does the opposite of that:
grep -F -x -v -f fileB fileA
But, is there a way to modify it so that I can reach my desired output?
Thank you
Hello owwow14,
Here is an awk
approach for same.
awk 'FNR==NR{A[$1]=$0;next} ($1 in A){print A[$1]}' fileA fileB
Output will be as follows.
adventures com|hum
adversity com|hum|hum
Thanks,
R. Singh
1 Like
awk 'FILENAME=="FileA" {arr[$1]++; next}
FILENAME=="FileB" {if($1 in arr) {print $1} }' FileA FileB >output
awk works well for this
awk 'FNR==NR{A[$1];next}$1 in A' FileB FileA
Hello Akshay,
I think you need to assign the value to A[$1]=$0
. As it will give only the first column of fileB.
awk 'FNR==NR{A[$1]=$0;next} ($1 in A){print A[$1]}' fileA fileB
Thanks,
R. Singh
Could you please explain me Mr.RavinderSingh13 why I need to assign the value unnecessarily ? I read FileB first not FileA please look into the post.
Thanks by the way :o
1 Like
Hello Akshay,
I got your point. Thank you for explaining me same.
Thanks,
R. Singh
No surprise man if you reverse files
RudiC
9
Why not
grep -ffileB fileA
adventures com|hum
adversity com|hum|hum
sea
10
I like things that are easy to read, imho makes it simpler to maintain later on...
while read line;do
grep ^$line[[:space:]] fileA
done<fileB