matching each line in a column to an array


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers matching each line in a column to an array
# 1  
Old 11-10-2008
matching each line in a column to an array

Hi,

I have two files:

file 1 has a list :

ABC_1024
ABC_4507
ABC_5589
ERT_4389
DDB_5507

file 2 has an array:

ABC_1011,ABC_1024,ABC_5670 DDB_4567 ERT_2345 January08
ABC_9099 DDB_9087 ERT_4567 March07

I need to go line by line in file 1 and match each entry to file 2. If there is a match like ABC_1024 then I need to output the whole line that matches from file 2. So my output would be:

ABC_1011,ABC_1024,ABC_5670 DDB_4567 ERT_2345 January08
# 2  
Old 11-10-2008
Is this what you are trying to do?

Code:
> cat file1
ABC_1024
ABC_4507
ABC_5589
ERT_4389
DDB_5507

> cat file2
ABC_1011,ABC_1024,ABC_5670 DDB_4567 ERT_2345 January08
ABC_9099 DDB_9087 ERT_4567 March07

> egrep -f file1 file2
ABC_1011,ABC_1024,ABC_5670 DDB_4567 ERT_2345 January08

# 3  
Old 11-11-2008
Thanks so much! It worked but, only returned one of the matches between file 1 and 2 , and I know there are many more. Any idea why it's not returning all of them?
Thanks again
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace Value of nth Column of Each Line Using Array

Hello All, I am writing a shell script with following requirement: 1. I have one input file as below CHE01,A,MSC,INO CHE02,B,NST,INC CHE03,C,STM,INP 2. In shell script I have predefined array as below: Array1={A, B, C} Array2= {U09, C04, A054} (6 Replies)
Discussion started by: angshuman
6 Replies

2. Shell Programming and Scripting

Matching column value from 2 different file using awk and append value from different column

Hi, I have 2 csv files. a.csv HUAWEI,20LMG011_DEKET_1296_RTN-980_IDU-1-11-ISV3-1(to LAMONGAN_M),East_Java,20LMG011_DEKET_1296_RTN-980_IDU-1,20LMG011,20LMG 027_1287_LAMONGAN_RTN980_IDU1,20LMG027,1+1(HSB),195.675,20LMG011-20LMG027,99.9995,202.6952012... (7 Replies)
Discussion started by: tententen
7 Replies

3. Shell Programming and Scripting

Matching column then append to existing File as new column

Good evening I have the below requirements, as I am not an experts in Linux/Unix and am looking for your ideas how I can do this. I have file called file1 and file2. I need to get the second column which is text1_random_alphabets and find that in file 2, if it's exists then print the 3rd... (4 Replies)
Discussion started by: mychbears
4 Replies

4. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

5. Shell Programming and Scripting

Find lines with matching column 1 value, retain only the one with highest value in column 2

I have a file like: I would like to find lines lines with duplicate values in column 1, and retain only one based on two conditions: 1) keep line with highest value in column 3, 2) if column 3 values are equal, retain the line with the highest value in column 4. Desired output: I was able to... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

6. Shell Programming and Scripting

Insert lines above matching line with content from matching

Hi, I have text file: Name: xyz Gender: M Address: "120_B_C; ksilskdj; lsudlfw" Zip: 20392 Name: KLM Gender: F Address: "65_D_F; wnmlsi;lsuod;,...." Zip:90233I want to insert 2 new lines before the 'Address: ' line deriving value from this Address line value The Address value in quotes... (1 Reply)
Discussion started by: ysrini
1 Replies

7. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

8. Shell Programming and Scripting

perl: array matching

Hi, I have two files like this file 1: xxtcgtatccgaggga cgcgcgggggagg jjsjjjjsjjjdtcgtat aaaaaaacccaaan ggtcgtatffaadda gggctggalllslllssdkk file 2: tcgtat gctggaI want to 1) match each element of file2 to each element of file1. 2) delete all the matched alphabets and subsequent... (3 Replies)
Discussion started by: polsum
3 Replies

9. Shell Programming and Scripting

Matching part of a string that is in an array

I am trying to do a comparison to see if these two string arrays match. I have tried assigning the array variable to another variable to get it to work but i can't figure it out. Here is what I have. #example of items that could be in the array #string="TEST"... (3 Replies)
Discussion started by: zatarra777
3 Replies

10. Shell Programming and Scripting

perl array matching between area code and no.

Hi Everyone, I have: my @No=qw(032106 032630 0380 034010 035110 0354801111); my $str_No=join(';', @No); I have a string $strA="03263033", so in order to determine this $strA area code matches with @No, I can do: if ( (rindex($str_No,substr($strA,0,5))))== -1) ) { print "Not... (1 Reply)
Discussion started by: jimmy_y
1 Replies
Login or Register to Ask a Question