Match string from two files and print line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match string from two files and print line
# 1  
Old 05-10-2017
Match string from two files and print line

Hi,
I have been trying to find help with my issue and I'm thinking awk may be able to do it.
I have two files eg
Code:
file1.txt
STRING1    230    400    0.36
STRING2    400    230    -0.13
STRING3    130    349      1

file2.txt

CUFFFLINKS    1   1394    93932   .   +     STRING1
CUFFFLINKS    1   94055   96078   .   +     STRING1
CUFFFLINKS    1   7654    9000   .     +     STRING3
CUFFFLINKS    1   6544    93932   .   +     STRING4

desired.txt
CUFFFLINKS    1   1394    93932   .   +    STRING1
CUFFFLINKS    1   94055   96078   .   +    STRING1
CUFFFLINKS    1   7654    9000   .   +     STRING3

I would like to loop through all entries in column 1 of file 1 and if the string matches any entry in column 7 of file 2 to print out the line of file 2

I have tried:
Code:
awk 'NR == FNR { a[$1]++ } NR != FNR { for (e in a) for (i=1;i<NF;i++) if (e ~ $i) print $0 }' file1.txt file2.txt

but this doesn't seem to work.

My understanding is that NR will only == FNR when the first file is read in so this populates the 'a' array. Then when NR != FNR (eg when the second file is read in) then there is a loop to try to match every element of 'a' and if this matched to print out the line. I can't see how I can get this specific to column 7 in the second file??
I'm a complete beginner so any help would be really appreciated!
Thanks.

Last edited by RudiC; 05-11-2017 at 02:35 AM.. Reason: Added code tags
# 2  
Old 05-10-2017
Welcome to the forum.
Thanks for (partly) using CODE tags, but please do so consistently.
Did you consder the links at the bottom left of this page? They usually offer a good starting point...
Howsoever, try
Code:
awk 'NR == FNR {a[$1]; next} $7 in a' file1 file2
CUFFFLINKS    1   1394    93932   .   +     STRING1
CUFFFLINKS    1   94055   96078   .   +     STRING1
CUFFFLINKS    1   7654    9000   .     +     STRING3

# 3  
Old 05-10-2017
Thanks RudiC. It's not quite working and I think it might know why. I've realised column 7 in file two is actually wrapped in ""; So I'm guessing the strings won't exactly match? eg

Code:
file2.txt
CUFFFLINKS    1   1394    93932   .   +     "STRING1";
CUFFFLINKS    1   94055   96078   .   +     "STRING1";
CUFFFLINKS    1   7654    9000   .     +     "STRING3";
CUFFFLINKS    1   6544    93932   .   +     "STRING4";

Would this prevent the strings from matching? If so, how can I remove these before performing the match? I thought about trying to open file 2, remove them with sed somehow and pipe into the command that you suggested but I can't do this as awk takes in file2 at the end of the command???
# 4  
Old 05-10-2017
Code:
awk 'FNR==NR {f1[$1];next} $(NF-1) in f1' file1 FS='"' file2

# 5  
Old 05-10-2017
Thanks vgersh99 but I can't quite get this to work. Could you explain the code?
# 6  
Old 05-10-2017
It's very similar to tge RuduC's solution with the exception of of the FiledSeparator [FS] being double-quote when file2 is processed.
it works with the files you've posted so far. What files are passing through and what do you get as output?

Sent from my Lenovo B8080-F using Tapatalk
# 7  
Old 05-10-2017
Apologies, this is my first post. I thought it would be easier to explain with an ammended file but I've learnt this confuses things. My file 2 looks like this.

Code:
1       StringTie       exon    18887   19382   .       +       .       transcript_id "MSTRG.5.1"; gene_id "MSTRG.5"; exon_number "1";
1       StringTie       exon    189836  191490  .       +       .       transcript_id "MSTRG.5.1"; gene_id "MSTRG.5"; exon_number "2";
1       StringTie       exon    18887   19382   .       +       .       transcript_id "MSTRG.49.4"; gene_id "MSTRG.5"; exon_number "1";
1       StringTie       exon    189836  191490  .       +       .       transcript_id "MSTRG.49.4"; gene_id "MSTRG.5"; exon_number "2";

If I
Code:
 awk '{print$10} file2

this prints out the "MSTRG###"; string that I want to match to (I don't want to match the string from gene_id.

Last edited by Don Cragun; 05-10-2017 at 11:59 PM.. Reason: Add ICODE tags.
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 script to match string and print status

Dear team, Need support to built awk script for below requirement Input file LOTC cluster state: ------------------- Node safNode=SC_2_1 joined cluster | Node safNode=SC_2_2 joined cluster | Node safNode=PL_2_3 fail cluster | AMF cluster state: ------------------... (16 Replies)
Discussion started by: shanul karim
16 Replies

2. Shell Programming and Scripting

Print next line beside preceding line on column match

Hi, I have some data like below: John 254 Chris 254 Matt 123 Abe 123 Raj 487 Moh 487 How can i print it using awk to have: 254 John,Chris 123 Matt,Abe 487 Raj,Moh Thanks. (4 Replies)
Discussion started by: james2009
4 Replies

3. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

4. Shell Programming and Scripting

Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

5. Shell Programming and Scripting

Print string after the word match

Hi, I have the logs : cat logsx.txt 744906,{"reportingGroups":,"version":"2.0"} 678874,{"reportingGroups":,"version":"2.0"} 193571,{"reportingGroups":,"version":"2.0"} 811537,{"reportingGroups":,"version":"2.0"} 772024,{"reportingGroups":,"version":"2.0"}... (5 Replies)
Discussion started by: justbow
5 Replies

6. Shell Programming and Scripting

Print lines that match regex on xth string

Hello, I need an awk command to print only the lines that match regex on xth field from file. For example if I use this command awk -F"|" ' $22 == "20130117090000.*" 'It wont work, I think, because single quotes wont allow the usage of the metacharacter star * . On the other hand I dont know... (2 Replies)
Discussion started by: black_fender
2 Replies

7. Shell Programming and Scripting

How to print everything after a string match

Hi all, I'm trying to do some work on the authorized_keys file to do a check if there's any information after the hash key.. At the end of the hash key's in the file, there can be an = or == Is there a way to check if anything exists after these equals and if so print it out or else print... (2 Replies)
Discussion started by: Jazmania
2 Replies

8. Shell Programming and Scripting

Print Line if next line Match a pattern

Hi All, Does anyone know how to print 1H1A....... in peal script print line ^1H1A....... if next line equal 5R0RECEIPT.... Thank for help:D Cat st.txt 1H1A-IN-11-5410-0009420|1010047766|dsds|1|N|IN|IN|000000|1||N|<<<line match 5R0RECEIPT| 5R0RECEIPT|... (2 Replies)
Discussion started by: kittiwas
2 Replies

9. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

10. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies
Login or Register to Ask a Question

Featured Tech Videos