Find all matched lines from two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find all matched lines from two files
# 1  
Old 04-14-2017
Find all matched lines from two files

Hello, everyone

I have two files like this:

File 1:
Code:
A              
B                
C
D
E
F

File 2:
Code:
A B 1
A C 2
A K 3
B A 4
D E 3
W X 2 
A B 2

I want to print all lines (file2) that the first two columns are consist of elements from file1.
So, what I expected is :

Code:
A B 1
A B 2
A C 2
B A 4
D E 3

Could anyone help me ? Thanks in advance.
# 2  
Old 04-14-2017
Hi, try:
Code:
awk 'NR==FNR{A[$1]; next} $1 in A && $2 in A' file1 file2

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-14-2017
Quote:
Originally Posted by Scrutinizer
Hi, try:
Code:
awk 'NR==FNR{A[$1]; next} $1 in A && $2 in A' file1 file2

great, thanks very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find files containing two specific lines and delate those lines?

Hi I need to find files in a specified folder where are two specified lines of text and delate that lines. It looks like this" 35. ?>NL 36. <iframe>.......</iframe>NLThe problem is that "?>" is in the other lines and id should not be removed if the next line is not like "<iframe>....." So... (4 Replies)
Discussion started by: androwida
4 Replies

2. UNIX for Dummies Questions & Answers

Extracting the two lines where the first line is matched

Hi, If I have a file of something like @hg19_gold_AL122127.6-131160 GCTTCATCATGCATGGATAGGCTGGCGCCTTTCCTGAGGCCATATGCCGATGGATATG @hg19_gold_AL122127.6-131159 CTTTAATATTTCCGCCACCATCCTGAGTGAATCCCAGCAAGGACAGTCTTTGGGGATT @hg19_gold_AL122127.6-131158... (4 Replies)
Discussion started by: jyu429
4 Replies

3. Shell Programming and Scripting

Find matched patterns in a column of 2 files with different size and merge them

Hi, i have input files like below:- input1 Name Seq_ID NewID Scores MT1 A0QZX3 1.65 277.4 IVO A0QZX3 1.65 244.5 HPO A0QZX3 1.65 240.5 RgP A0Q3PP 5.32 241.0 GX1 LPSZ3S 96.1 216.9 MEL LPSS3X 4.23 204.1 LDD LPSS3X 4.23 100.2 input2 Fac AddName NewID ... (9 Replies)
Discussion started by: redse171
9 Replies

4. Shell Programming and Scripting

Find matched patterns in multiple files

Hi, I need help to find matched patterns in 30 files residing in a folder simultaneously. All these files only contain 1 column. For example, File1 Gr_1 st-e34ss-11dd bt-wwd-fewq pt-wq02-ddpk pw-xsw17-aqpp Gr_2 srq-wy09-yyd9 sqq-fdfs-ffs9 Gr_3 etas-qqa-dfw ddw-ppls-qqw... (10 Replies)
Discussion started by: redse171
10 Replies

5. Shell Programming and Scripting

Delete lines and the first pattern between 2 matched patterns

Hi, i need help to delete all the lines between 2 matched patterns and the first pattern must be deleted too. sample as follows: inputfile.txt >kump_1 ........................... ........................... >start_0124 dgfhghgfh fgfdgfh fdgfdh >kump_2 ............................. (7 Replies)
Discussion started by: redse171
7 Replies

6. Shell Programming and Scripting

Grep command to return all the lines between one matched pattern to another.

14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. 14:50:00- abcdefghijkl. 123456789. 15:30:00-abcdefghijkl. (3 Replies)
Discussion started by: dev_shivv
3 Replies

7. Shell Programming and Scripting

removing lines around a matched pattern

I have an ugly conf file that has the string I'm interested in searching for in the middle of a block of code that's relevant, and I'm trying to find a way to remove that entire block based on the matched line. I've googled for this problem, and most people helping are only interested in... (9 Replies)
Discussion started by: tamale
9 Replies

8. Shell Programming and Scripting

How to get the lines matched of a file in perl?

Hi, I want to match the time in the file and retrieve those contents of the file. I am taking only first two parameters of localtime(time) function minutes and seconds so partial match i am performing. For Example $start = "14:23"; $end = "14:30"; I am matching file contents... (3 Replies)
Discussion started by: vanitham
3 Replies

9. Shell Programming and Scripting

Sed grep the first matched lines

Hi I have a myfile.txt contains the following: CONTEXT { AAAAA } ... CONTEXT { BBBBB } I want to extract the lines in between CONTEXT { ... }, one by one. Hence I wrote a command like the following, sed -n '/^CONTENT/,/^}/ { w a.txt }' myfile.txt The problem with this... (5 Replies)
Discussion started by: hezjing
5 Replies

10. Shell Programming and Scripting

print only matched string instead lines in grep

frnd, Is there any way to print only the string that matched the pattern instead printing the whole line? thanks in advance. (3 Replies)
Discussion started by: clx
3 Replies
Login or Register to Ask a Question