Awk:Comparing and matching two file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk:Comparing and matching two file
# 1  
Old 11-29-2012
Awk:Comparing and matching two file

Dear All,
I have 2 files as follows
file1.txt
Code:
261187210101|r
261187210101|r
261187220101|y
261187220102|y
261187220103|y
261187231011|b
261187231011|b
261187241012|g
261187241012|g

file2.txt
Code:
1187220
1187241

output:

file1 |file2 |match
Code:
261187220101|y 1187220 |ok
261187220102|y 1187220 |ok
261187220103|y 1187220 |ok
261187241012|g 1187241 |ok
261187241012|g 1187241 |ok

how to match the two files above

Last edited by Franklin52; 11-29-2012 at 08:50 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 11-29-2012
Please use code tags for code and data sample.

Try

Code:
awk 'NR==FNR{A[$0]=$0;next}
FNR==1{print "file1 |file2 |match"}
{for(i in A){if($0 ~ i){print $0,i,"|ok"}}}' file2 file1

# 3  
Old 11-29-2012
Code:
awk 'FNR==NR{a[$1];next}
{for(i in a)
  if(index($1,i)) {
   print $0,i,"|ok"
   break
  }}' file2 FS=\| file1

# 4  
Old 11-29-2012
Awk works well,

Thank's Pamu and Elixir_sinari .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Comparing two files by two matching fields

Long time listener first time poster. Hope someone can advise. I have two files, 1000+ lines in each, two fields in each file. After performing a sort, what is the best way to find exact matches where field $1 and $2 in file1 are also present in file2 on the same line, then output only those... (6 Replies)
Discussion started by: bstaff
6 Replies

3. Shell Programming and Scripting

awk to update file with sum of matching fields in another file

In the awk below I am trying to add a penalty to a score to each matching $1 in file2 based on the sum of $3+$4 (variable TL) from file1. Then the $4 value in file1 is divided by TL and multiplied by 100 (this valvue is variable S). Finally, $2 in file2 - S gives the updated $2 result in file2.... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk - writing matching pattern to a new file and deleting it from the current file

Hello , I have comma delimited file with over 20 fileds that i need to do some validations on. I have to check if certain fields are null and then write the line containing the null field into a new file and then delete the line from the current file. Can someone tell me how i could go... (2 Replies)
Discussion started by: goddevil
2 Replies

5. Shell Programming and Scripting

Comparing two csv file fields using awk script

Hi All, I want to remove the rows from File1.csv by comparing the columns/fields in the File2.csv. I only need the records whose first column is same and the second column is different for the same record in both files.Here is an example on what I need. File1.csv: RAJAK|ACTIVE|1... (2 Replies)
Discussion started by: rajak.net
2 Replies

6. Shell Programming and Scripting

comparing two files for matching fields

I am newbie to unix and would please like some help to solve the task below I have two files, file_a.text and file_b.text that I want to evaluate. file_a.text 1698.74 1711.88 6576.25 899.41 3205.63 4187.98 697.35 1551.83 ... (3 Replies)
Discussion started by: gameli
3 Replies

7. Shell Programming and Scripting

AWK file comparing and composing

Hi all, I'm less than newbie with AWK, but I'd like to learn more with your help! This is my problem. I've two files. File A test_a component_b test_b component_k test_c component_d test_g component_b test_k component_a .... The first column is a key (there is ONE test_a... (10 Replies)
Discussion started by: aprile24
10 Replies

8. Shell Programming and Scripting

AWK - Comparing/Matching/Counting with 2 files

I have 2 files that I want to do some comparing on. First, I want to find the unique list of devices in file1 and then put them to a new file, file2. I was able to do this without any problem with the following statement: cat file1 | awk '{print $2}' | awk '!x++' > file2Here is what I can't... (2 Replies)
Discussion started by: jontjioe
2 Replies

9. Shell Programming and Scripting

Comparing 2 files with awk and updating 2nd file

file1: (unique files) 1 /pub/atomicbk/catalog/catalog.gif 693 2 /pub/atomicbk/catalog/home.gif 813 3 /pub/atomicbk/catalog/logo2.gif 12871 4 /pub/atomicbk/catalog/sleazbk.html 18338 file2: (duplicate filenames allowed) 28/Aug/1995:00:00:38 1 /pub/atomicbk/catalog/home.gif 813... (2 Replies)
Discussion started by: jontjioe
2 Replies

10. Shell Programming and Scripting

Comparing 2 csv files and matching content

Hello, I have the following problem: There are two csv files csv-file #1: aaa1, aaa2, ... aaan aaa1, bbb2, ... bbbn aaa1, ccc2, ... cccn bbb1, bbb2, ... bbbn ... zzz1, zzz2, ... zzzn csv-file #2: aaa1, matchvalue1 ccc1, matchvalue2 (7 Replies)
Discussion started by: ghl10000
7 Replies
Login or Register to Ask a Question