Python fails to detect String Match Found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python fails to detect String Match Found
# 1  
Old 05-19-2016
BSD Python fails to detect String Match Found

Below is my code for comparing string for Exact Match found in python.

Code:
    for word in jdbc_trgt.split(','):
        global comp
        comp=word.strip();
        print "GloBAL:" + comp
        fiIn = open('list.txt').readlines()
        for lines in fiIn:
            print "line1s:" + lines
            print comp + " == " + lines
            if comp == lines:
                print "MATCH:" + comp

Quote:
Output:

GloBAL:MGT
line1s:MGT
MGT == MGT
Can you tell me why is print "MATCH:" + comp not getting printed despite the strings Matching ?
Note: I am looking for an EXACT match Found scenario.

Last edited by mohtashims; 05-19-2016 at 11:21 AM..
# 2  
Old 05-19-2016
Code:
if comp == lines.strip():


Last edited by rdrtx1; 05-19-2016 at 03:12 PM..
# 3  
Old 05-19-2016
Quote:
Originally Posted by rdrtx1
Code:
if comp.find(lines, 0, len(comp)):

Does not work.

It is is now Matching everything despite them being very different.

Output:
Quote:
GloBAL:MGT
line1s:TING

MGT == TING

MATCH:MGT
Note: I need exact Match.

---------- Post updated at 12:29 PM ---------- Previous update was at 10:12 AM ----------

I even tried

Code:
if str(comp) == str(lines):

But still no Luck. Can someone help please ?
# 4  
Old 05-19-2016
What is your input data?
# 5  
Old 05-19-2016
I am split a string into words using a delimiter [check here: Issue Spliting String in Python ] and storing it in variable
Quote:
"comp"
variable while the other words are read from a text file for comparison.

So i am split a string into words and then checking if that word is present in the text file or not.

Last edited by mohtashims; 05-19-2016 at 03:30 PM..
# 6  
Old 05-19-2016
Code:
if comp == lines.strip():

This User Gave Thanks to rdrtx1 For This Post:
# 7  
Old 05-19-2016
Quote:
Originally Posted by rdrtx1
Code:
if comp == lines.strip():

This also does not work.

I get the below error.

Code:
Problem invoking WLST - Traceback (innermost last):
  (no code object) at line 0
  File "/web/jdbc.py", line 70
                     if comp == lines.strip():
                     ^
SyntaxError: invalid syntax

---------- Post updated at 02:38 PM ---------- Previous update was at 01:36 PM ----------

Quote:
Originally Posted by rdrtx1
Code:
if comp == lines.strip():

This worked !! Thank you !!

---------- Post updated at 02:39 PM ---------- Previous update was at 02:38 PM ----------

Resolved
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

E**(i*pi)=-1 or e**(j*pi)=-1, something I found out in Python using part of Eulers Identit equation.

Well guys and gals I have discovered after all these years that Python does complex numbers without the 'complex()' function or 'cmath' import. It is well known that Euler's Identity E**(i*pi)+1=0 so I decided to experiment Last login: Fri Dec 13 18:27:30 on ttys000 AMIGA:amiga~> python3.8... (0 Replies)
Discussion started by: wisecracker
0 Replies

2. Programming

Python re.findall inverse Match

I ask of you but yet another simplistic question that I hope can be answered. Its better explained showing my code. Here is my list(tmp_pkglist), which contains a list of all Debian (Jessie) packages: snippet 'zssh (1.5c.debian.1-3.2+b1 , 1.5c.debian.1-3.2 )', 'zsync (0.6.2-1)', 'ztex-bmp... (2 Replies)
Discussion started by: metallica1973
2 Replies

3. Shell Programming and Scripting

Grep everything between two pattern if match not found

I need to help to work this Print everything between 2 patterns if grep is not found the search word example Detroit orange cat bat rat apple sed -n "/Detroit,/apple/p" d |grep chicago output would be Detroit orange cat bat rat (1 Reply)
Discussion started by: jhonnyrip
1 Replies

4. Shell Programming and Scripting

How to extract next n characters after a match is found?

Hi, I want to extract the next 7 characters after I encounter the first ( in the code eg abc123=(xvn1342) xyz678123=(ret8901) I want to extract xvn1342,ret8901. Please advise how to achieve this with awk, if possible? (9 Replies)
Discussion started by: sidnow
9 Replies

5. UNIX for Dummies Questions & Answers

Launch shell script if string match is found

I'm trying to write a simple shell script that looks at a given text file and if the only word in the file is 'completed', it launches another shell script. So far I have this almost working... if grep 'completed' $datafile then... however, using this logic the secondary shell script... (3 Replies)
Discussion started by: MickeyGreen
3 Replies

6. UNIX for Dummies Questions & Answers

Display n lines before match found

I have a file with following data A B C D E F G H I K L M N and search pattern is G Expected output (3 Replies)
Discussion started by: nsuresh316
3 Replies

7. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

8. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

9. 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

10. Shell Programming and Scripting

if match found go to a particular line in perl

Hello Experts, I am newbie to perl, just curious to know how to do the following in perl. suppose I ve a txt file like below. when it founds "*Main Start" Then go to "*Main End,,,,,,,," patteren and just collect the number from the previous line of "*Main End,,,,,,," pattern . In my... (17 Replies)
Discussion started by: user_prady
17 Replies
Login or Register to Ask a Question