Match and Extract


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Match and Extract
# 1  
Old 07-05-2005
Question Match and Extract

Hi,
I am having 2 files which are as follows :

File 1
1*001
2*002
3*003
4*004

File2
002
003

I want to match the entries in File 2 against File1.If an entry from File2 is found in File1 then I need to extract the number before "*" (a delimiter)
for example...i pick up 002 from File 2 and I search it in File 1.I see that it is present as a sub string in the entry "2*002".So now I will extract the number 2 present before the delimiter "*"

Note There can't be mulitple occurances of entry from File 2 in File1 that means in the example just taken, there can't be more than one 002.
Can you gurus suggest how is it supposed to be done ?
Thanks and Regards Smilie
Tushar Johri
# 2  
Old 07-05-2005
Code:
awk -F'*' 'NR==FNR{a[$0]}(NR!=FNR)&&($2 in a){print $1}' file2 file1

# 3  
Old 07-05-2005
Code:
 awk -vfile=file2 -f script.awk file1 | cut -d"*" -f 1



script.awk
Code:
    BEGIN { while( getline<file) arr[$0]=$0 }
{ 
	for (i in arr) {
	if ( $0 ~ i )
	{
		print $0
	}
}
}

# 4  
Old 07-05-2005
Hi r2007 and bhargav,
As I am new to this jargon of Unix and Shell scripting , please help me figure out how the piece of the code works
Thanks
Tushar
# 5  
Old 07-05-2005
Perhaps use grep instead...
Code:
fgrep -f file2 file1 | cut -d"*" -f 1

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match patterns between two files and extract certain range of strings

Hi, I need help to match patterns from between two different files and extract region of strings. inputfile1.fa >l-WR24-1:1 GCCGGCGTCGCGGTTGCTCGCGCTCTGGGCGCTGGCGGCTGTGGCTCTACCCGGCTCCGG GGCGGAGGGCGACGGCGGGTGGTGAGCGGCCCGGGAGGGGCCGGGCGGTGGGGTCACGTG... (4 Replies)
Discussion started by: bunny_merah19
4 Replies

2. UNIX for Beginners Questions & Answers

Match file and extract the output

Hi All I have 2 file . I need match the files based on key and then form a third file which have the matching values FILE1: 10264;K*AD 10265;K*AIR 10266;K*AUTO 10267;K*BABY 10268;K* FOOD FILE2: 10264;1055.83 10265;716.94 10267;331.80 10268;23283.33 OUTPUT (Needed)... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

3. UNIX for Beginners Questions & Answers

Help with pattern match and Extract

Hi All, I am having a file like below . Basically when SB comes in the text with B. I have to take the word till SB. When there only B I should take take till B. Tried for cut it by demilter but not able to build the logic SB- CD B_RESTO SB_RESTO CRYSTALS BOILERS -->There SB and B so I... (6 Replies)
Discussion started by: arunkumar_mca
6 Replies

4. Shell Programming and Scripting

Match and extract data using two files

Hello, Using the information in file 1, I would like to extract from file2 all rows which matchs in column 3. file 1 1233 1230 1231 1232 file2 65733.00 19775.00 1220 65733.00 19793.00 1220 65733.00 19801.00 1220 65733.00 19809.00 1231 65733.00 19817.00 ... (2 Replies)
Discussion started by: jiam912
2 Replies

5. Shell Programming and Scripting

How to extract next n characters after a match is found?

Hi, I want to extract the next 7 characters after I encounter the first ( in the code eg abc123=(xvn1342) xyz678123=(ret8901) I want to extract xvn1342,ret8901. Please advise how to achieve this with awk, if possible? (9 Replies)
Discussion started by: sidnow
9 Replies

6. Shell Programming and Scripting

Extract lines that match a pattern

Hi all, I got a file that contains the following content, Actually it is a part of the file content, Installing XYZ XYZA Image, API 18, revision 2 Unzipping XYZ XYZA Image, API 18, revision 2 (1%) Unzipping XYZ XYZA Image, API 18, revision 2 (96%) Unzipping XYZ XYZA Image, API 18,... (7 Replies)
Discussion started by: Kashyap
7 Replies

7. Shell Programming and Scripting

Pattern Match & Extract from a string

Hi, I have long string in 2nd field, as shown below: REF1 | CLESCLJSCSHSCSMSCSNSCSRSCUDSCUFSCU7SCV1SCWPSCXGPDBACAPA0DHDPDMESED6 REF2 | SBR4PCBFPCDRSCSCG3SCHEBSCKNSCKPSCLLSCMCZXTNPCVFPCV6P4KL0DMDSDSASEWG I have a group of fixed patterns which can occur in these long strings & only... (11 Replies)
Discussion started by: karumudi7
11 Replies

8. Shell Programming and Scripting

Extract match cases

hi guys i have a file like this which is contain more than 3000 records i want to extract emails have Creation Date: : Wed, 14 Aug 2009 and their POP3 Last Login Date and WebMail Last Login Date is Thu, 01 Jan 1970 how can i could do that for example in this sample i need... (13 Replies)
Discussion started by: mhs
13 Replies

9. Shell Programming and Scripting

Extract all content that match exactly only specific word

Input: 21 templeta parent 35718 36554 . - . ID=parent_cluster_50.21.11; Name=Partial%20parent%20for%20training%20set; 21 templeta kids 35718 36554 . - . ID=_52; Parent=parent_cluster_5085.21.11; 21 templeta ... (7 Replies)
Discussion started by: patrick87
7 Replies

10. Shell Programming and Scripting

Extract data from records that match pattern

Hi Guys, I have a file as follows: a b c 1 2 3 4 pp gg gh hh 1 2 fm 3 4 g h i j k l m 1 2 3 4 d e f g h j i k l 1 2 3 f 3 4 r t y u i o p d p re 1 2 3 f 4 t y w e q w r a s p a 1 2 3 4 I am trying to extract all the 2's from each row. 2 is just an example... (6 Replies)
Discussion started by: npatwardhan
6 Replies
Login or Register to Ask a Question