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
# 8  
Old 05-10-2017
Quote:
Originally Posted by zward
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".
try - not tested (yet)
Code:
awk 'FNR==NR {f1[$1];next} $2 in f1' file1 FS='"' file2

Sent from my Lenovo B8080-F using Tapatalk

---------- Post updated at 10:41 PM ---------- Previous update was at 10:36 PM ----------

sorry - counted the fields wrong and don't have the matching file1, but:
Code:
awk 'FNR==NR {f1[$1];next} $4 in f1' file1 FS='"' file2

It should be either $2 or $4 depending on what MSTRG you want to match on......
# 9  
Old 05-10-2017
Can you explain the code ( the $4??)
My file 1 is like:
Code:
MSTRG.1.1       233     0       0       0
MSTRG.5.1       2151    300     0.7186  
MSTRG.13.1      1705    261     0.4076
MSTRG.49.1      1746    357     1.189
MSTRG.50.1      1809    273     1.0285
MSTRG.50.2      890     201     1.1133  
MSTRG.50.3      466     75      0.7497  
MSTRG.49.4      1743    246     1.0052
MSTRG.49.5      885     246     1.0052

# 10  
Old 05-10-2017
give file1 and file2 posted above, the code should be:
Code:
awk 'FNR==NR {f1[$1];next} $2 in f1' file1 FS='"' file2

The FieldSeparator (FS) for file2 is ". When file2 is processed ($2 in f1), given FS="'", $2 becomes the FIRST quoted string withOUT the quotes.
Not sure if my explanation is consumable tho Smilie
This User Gave Thanks to vgersh99 For This Post:
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