Match strings in 2 different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match strings in 2 different files
# 1  
Old 12-13-2013
Match strings in 2 different files

Hi,

i am trying to match strings from 2 different files based on position like below:-

file1 (tab delimited)
Code:
f07270 lololol  fff
u12730 gggddd  dddkkrr  mmm


file2 (not tab delimited)
Code:
%f07270 APSLH bl%alalalalallaadsdsfdfdfdgsgfss
%g13450 GDIDFLRIP%ILITEAPPRKgsfgsgsf
%d08880 pve1_%00%39444gsgfsg
ታ0 tog(%s)gsfgfss

file3 (output)
Code:
%f07270 APSLH bl%alalalalallaadsdsfdfdfdgsgfss
ታ0 tog(%s)gsfgfss

the code:
Code:
awk 'FNR==NR{_[substr($0,1,6)]; next}{for(i in _){if(i==substr($0,1,7)){print $0}}}' file1 file2 > file3

am i missing something as i just got blank file for the output?. Kindly help, thanks.

Last edited by Don Cragun; 12-13-2013 at 10:50 PM.. Reason: Change QUOTE tags to CODE tags.
# 2  
Old 12-13-2013
Code:
awk 'FNR==NR{_[substr($0,1,6)]; next}{for(i in _){if(i==substr($0,1,7)){print $0}}}' file1 file2 > file3

Should be:
Code:
awk 'FNR==NR{_[substr($0,1,6)]; next}{for(i in _){if(i==substr($0,2,6)){print $0}}}' file1 file2 > file3

Or shorter:
Code:
awk 'FNR==NR{_[$1]; next} substr($0,2,6) in _' file1 file2 > file3

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 12-13-2013
OR

Code:
$ awk -F'[% ]' 'FNR==NR{A[$1];next}$2 in A' file1 file2

This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 12-13-2013
Quote:
Originally Posted by Akshay Hegde
OR

Code:
$ awk -F'[% ]' 'FNR==NR{A[$1];next}$2 in A' file1 file2

Works only if the second file has a space as field separator as in the provided example, but who knows?
This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 12-13-2013
Quote:
Originally Posted by Franklin52
Works only if the second file has a space as field separator as in the provided example, but who knows?
Yes Franklin52, thats true. but I thought it would be better if we do not use substr($0,2,6) since we really do not know how many char ? whether its of fixed length or not in real data.

---edit---

OR this could be an alternative if space separated
Code:
$ awk  'FNR==NR{A[$1];next}substr($1,2) in A'  file1 file2


Last edited by Akshay Hegde; 12-13-2013 at 03:52 PM..
This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 12-13-2013
Hi guys,

it takes some time for me to reply as i am still working on the codes and my data. Actually the codes are working fine but i have another issue. My file2 is huge and contains variety of data. The issue is not all of the data are printed out due to "new line". I want all data to be printed out starting from "%" and will stop when the next "%" is reached. for example,

correct output file
Code:
%f07270 APSLH bl%alalalalallaadsdsfdfdfdgsgfss
ffffffffffgggggggggggggrrrrrrrrrrrrrwwwwww333r22356676
ujhjhkjkuuuuuuuuu2228888fddddddddddddddddererererer30tog
ታ0 tog(%s)gsfgfss
ffffffffddddddddddssssssssssffffffffffgggggggggghhhhhhhhy
ssssssssssssfdfgggggggghhhhhhhhhhhhhhhhhhhhrrrrreee

for the given codes, i got results only until end of line. i checked awk manual from book and internet and tried couple of things by amending your codes to get the desired output but failed. am sure there should be a simple solution for this but i just couldnt figure it out. my mistake to give u short sample of data

Last edited by redse171; 12-13-2013 at 08:35 PM.. Reason: typo
# 7  
Old 12-13-2013
Could you specify samples of the input files that would need to lead to that output?
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

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. UNIX for Beginners Questions & Answers

Match Strings between two files, print portions of each file together when matched ([g]awk)

I have two files and desire to use the strings from $1 of file 1 (file1.txt) as search criteria to find matches in $2 of file 2 (file2.txt). If matches are found I want to output the entire line of file 2 (file2.txt) followed by fields $2-$11 of file 1 (file1.txt). I can find the matches, I cannot... (7 Replies)
Discussion started by: jvoot
7 Replies

4. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

5. Shell Programming and Scripting

Using awk to match strings and outputing their corresponding values

Hi I will appreciate it if you can help me out. I have a file that contains this data System Load: 3244 card: 1903 CPU: 6% card: 1904 CPU: 6% card: 1905 CPU: 28% card: 1906 CPU: 28% card: 1907 CPU: 36% card: 1908 CPU: 37% I need to manipulate and output this as system_load:3244... (2 Replies)
Discussion started by: kaf3773
2 Replies

6. Shell Programming and Scripting

Returning two lines if they both match strings

Hi I have a problem where I have a large amount of files that I need to scan and return a line and its following line, but only when the following line begins with a string. String one - line one must begin with 'Bill' String two - line two must begin with 'Jones'. If these two... (7 Replies)
Discussion started by: majormajormajor
7 Replies

7. UNIX for Dummies Questions & Answers

Grep help (match two strings one line)

Hello, Here I have some grep command which is not working correctly: cat file1.txt: apples Date: Sun, 24 Feb 2013 8:14:06 -0800 peaches melons cherry sky cloud green purple yellow cat file2.txt: apples Date peaches melons 0800 cherry sky cloud green purple black (2 Replies)
Discussion started by: holyearth
2 Replies

8. Shell Programming and Scripting

Match strings in two files and compare columns of both

Good Morning, I was wondering if anybody could tell me how to achieve the following, preferably with a little commenting for understanding. I have 2 files, each with multiple rows with multiple columns. I need to find each row where the value in column 1 of file 1 matches column 1... (10 Replies)
Discussion started by: GarciasMuffin
10 Replies

9. Shell Programming and Scripting

Parsing file to match strings

I have a file with the following format 12g data/datasets/cct 8g data/dataset/cct 10 g data/two 5g data/something_different 10g something_different 5g data/two is there a way to loop through this... (1 Reply)
Discussion started by: yawalias
1 Replies

10. UNIX for Dummies Questions & Answers

Regex in if-then-else statement to match strings

hello I want to do a pattern match for string in the if statement, but I am not sure how to use regex inside the if statement. I am looking for something like this: if {2,3} ]; then ..... .... ... fi (7 Replies)
Discussion started by: rakeshou
7 Replies
Login or Register to Ask a Question