Print only '+' or '-' if string matches (two files)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print only '+' or '-' if string matches (two files)
# 1  
Old 03-14-2014
Print only '+' or '-' if string matches (two files)

I would like to add two additional conditions to the actual code I have: print '+' if in File2 field 5 is greater than 35 and also field 7 is grater than 90.

Code:
while read -r line
do
    grep -q "$line" File2.txt && echo "$line +" || echo "$line -"
done < File1.txt '


Input file 1:

Code:
HAPS_0001
HAPS_0002
HAPS_0005
HAPS_0006
HAPS_0007
HAPS_0008
HAPS_0009
HAPS_0010

Input file 2 (tab-delimited):

Code:
Query	DEG_ID	E-value	Score	%Identity	%Positive	%Matching_Len
HAPS_0001	protein:plasmid:149679	3.00E-67	645	45	59	91
HAPS_0002	protein:plasmid:139928	4.00E-99	924	34	50	85
HAPS_0005	protein:plasmid:134646	3.00E-98	915	38	55	91
HAPS_0006	protein:plasmid:111988	1.00E-32	345	33	54	86
HAPS_0007	-	-	0	0	0	0
HAPS_0008	-	-	0	0	0	0
HAPS_0009	-	-	0	0	0	0
HAPS_0010	-	-	0	0	0	0

Desired output (tab-delimited):

Code:
HAPS_0001	+
HAPS_0002	-
HAPS_0005	+
HAPS_0006	-
HAPS_0007	-
HAPS_0008	-
HAPS_0009	-
HAPS_0010	-

Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output data as well as for code samples.

Thanks!

Last edited by Don Cragun; 03-14-2014 at 04:38 PM.. Reason: Add CODE tags.
# 2  
Old 03-14-2014
You can do all of this using a single awk script instead of "while grep and echo"...
# 3  
Old 03-14-2014
Try:
Code:
awk '$5>35&&$7>90{print $1,"+";next}{print $1,"-"}' File2.txt

This User Gave Thanks to bartus11 For This Post:
# 4  
Old 03-14-2014
Bash approach:
Code:
#!/bin/bash

declare -A ARR

while read line
do
        ARR["$line"]="$line"
done < File1.txt

while read query degid eval scr iden post matc
do
        [[ "$query" =~ ^Query ]] && continue
        if [ ! -z ${ARR["$query"]} ]
        then
                if [ $iden -gt 35 ] && [ $matc -gt 90 ]
                then
                        printf "%s\t+\n" "$query"
                else
                        printf "%s\t-\n" "$query"
                fi
        fi
done < File2.txt

This User Gave Thanks to Yoda For This Post:
# 5  
Old 03-16-2014
OR

Code:
$ awk 'NR > 1 { print $1,($5 >35 && $7 > 90) ? "+" : "-" }' File2.txt
HAPS_0001 +
HAPS_0002 -
HAPS_0005 +
HAPS_0006 -
HAPS_0007 -
HAPS_0008 -
HAPS_0009 -
HAPS_0010 -

# 6  
Old 03-16-2014
Hello,

Another approach with awk.

Code:
awk -vs1=35 -vs2=90 -vs3="+" -vs4="-" 'NR>1{if($5>s1 && $7>s2) {print $1 OFS s3} else {print $1 OFS s4}}' OFS="\t"  file_name

Output will be as follows.

Code:
HAPS_0001       +
HAPS_0002       -
HAPS_0005       +
HAPS_0006       -
HAPS_0007       -
HAPS_0008       -
HAPS_0009       -
HAPS_0010       -


Thanks,
R. Singh
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print entire line only if certain fixed character matches the string

Hi All, I have a file testarun.txt contains the below lines and i want to print the lines if the character positions 7-8 matches 01. 201401011111 201401022222 201402013333 201402024444 201403015555 201403026666 201404017777 201404028888 201405019999 201405020000 I am trying the... (4 Replies)
Discussion started by: Arunprasad
4 Replies

2. UNIX for Dummies Questions & Answers

Print Matches to New Columns

Hi all, I have a problem that I'm struggling to resolve. I have two files that look like this: File 1 654654654 3 987987987 2 321321321 1 File 2 14NS0064 654654654 14NS0054 654654654 14NS0032 654654654 14NS0090 987987987 14NS0093 987987987 14NS0056 321321321 As you may notice,... (2 Replies)
Discussion started by: winkleman
2 Replies

3. Shell Programming and Scripting

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

4. UNIX for Dummies Questions & Answers

Print only matches

Im not sure how to tell this awk command to only print when there is a match between the two files. Right now its printing the words in quotation even if there is not a match. I tried removing the additional info and it prints blank spaces?? awk ' { IP = $1 $1 = "" } FNR == NR { ... (2 Replies)
Discussion started by: sectech
2 Replies

5. Shell Programming and Scripting

Compare two text files and print matches

Hi, I am looking for a way to compare two text files and print the matches. For example; File1.txt 89473036 78474384 48948408 95754748 47849030 File2.txt 47849030 46730356 16734947 78474384 36340047 Output: (11 Replies)
Discussion started by: lewk
11 Replies

6. Shell Programming and Scripting

How to print line if field matches?

Hi all, I got several lines line this a b c d e 1 e a 1 c d e 3 f a b c 1 e 8 h a b c d e 1 w a 1 c d e 2 w a b c d e 1 t a b c d e 7 4 How can I print the line if 1 is the field one before the last field? Basicly this 2 field ? a b c d e 1 e a b c d e 1 t The file I got is... (7 Replies)
Discussion started by: stinkefisch
7 Replies

7. Shell Programming and Scripting

Print xml if value matches

Hello Gurus.... Can any one please let me know how to print entire xml if the vlaue in the xml matches to a variable given. In PERL. example: <rpad:systemId>abc</hcdd:systemId> <rpad:hcdd>1172</hcdd:hcdid> </get:Request></soapenv:Body></soapenv:Envelope> if value macthes 1172 I... (3 Replies)
Discussion started by: thankful123
3 Replies

8. Shell Programming and Scripting

If string matches within 2 files, delete one file.

I have a directory with a large # of files and in each file I am looking to match a string in one file with a string in the subsequent n file(s). If there is a match between a string in one file and a string in the next n file(s) then delete the subsequent duplicate file(s). Here is sample input: ... (2 Replies)
Discussion started by: sitney
2 Replies

9. Shell Programming and Scripting

print next line if matches a particular word..need help

Hi i need a help for making a script whch can print next line if it matches a particular word like file1 have ename Mohan eid 2008 ename Shyam eid 345 if scipt got Mohan it will print next line (eid 2008) pls help me .......:) (2 Replies)
Discussion started by: anish19
2 Replies

10. Shell Programming and Scripting

Looking for a string in files and reporting matches

Can someone please help me figure out what the command syntax I need to use is? Here is what I am wanting to do. I have hundreds of thousands of files I need to look for a specific search string in. These files are spread across multiple subdirectories from one main directory. I would like... (4 Replies)
Discussion started by: btrotter
4 Replies
Login or Register to Ask a Question