Search string in output

This is the place for queries that don't fit in any of the other categories.

Search string in output

Postby hellb0y77 » Mon Jun 16, 2014 8:17 am

Hi, i need to search string in a output and run a script when you find a string, is the my first script...it read a SQS queue in AWS and when it finds a string must run a script:

Code: Select all
#!/usr/bin/env python
 
import boto.sqs
import datetime
 
now = datetime.datetime.now()
now = str(now)
 
outfile = open("/root/SQS/messages.txt","a")
 
conn = boto.sqs.connect_to_region("eu-west-1",aws_access_key_id='...',aws_secret_access_key='....')
 
my_queue = conn.get_queue('queue_name')
 
rs = my_queue.get_messages()
 
if len(rs) == 0:
        outfile.write('{0} - No message\n'.format(now))
else:
        m = rs[0]
        outfile.write('{0} - {1}\n'.format(now, m.get_body_encoded()))
 
if len(rs) > 0:
       my_queue.delete_message(m)
 
outfile.close()


After
Code: Select all
outfile.write('{0} - {1}\n'.format(now, m.get_body_encoded()))
i enter a search string command, i can use "str.find"? is right if I insert
Code: Select all
str.m.get_body_encoded()
?
hellb0y77
 
Posts: 6
Joined: Thu Jun 12, 2014 8:30 am

Re: Search string in output

Postby stranac » Mon Jun 16, 2014 9:32 am

I have no idea what you're asking...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Search string in output

Postby snippsat » Mon Jun 16, 2014 3:10 pm

Your other post.
viewtopic.php?f=6&t=11739
i enter a search string command, i can use "str.find"? is right if I insert

No,your question is very unclear.
That line is writing to file and all is string(str).
You should doing some more study,so you have a little more understating whats's going on.
User avatar
snippsat
 
Posts: 296
Joined: Thu Feb 21, 2013 12:04 am

Re: Search string in output

Postby hellb0y77 » Mon Jun 16, 2014 3:26 pm

Let me explain, m.get_body_encoded() returns a text, I have to find a word in the text or a value, if it is found, launch a script or closes the program. Now, the question is: can I search directly in the output or do I save the output to a file and search within the file?
hellb0y77
 
Posts: 6
Joined: Thu Jun 12, 2014 8:30 am

Re: Search string in output

Postby snippsat » Mon Jun 16, 2014 3:46 pm

can I search directly in the output or do I save the output to a file and search within the file?

You have to search before writing to file,wish seems most logical,
or you have open "messages.txt" for read,and then do search.
User avatar
snippsat
 
Posts: 296
Joined: Thu Feb 21, 2013 12:04 am

Re: Search string in output

Postby mnutz » Mon Jun 16, 2014 4:38 pm

There's a few ways you could do it (if i am reading this correctly)

Add the text you want to a variable, then search that for the word:

var = "your string"

if var.count(word) >= 1:
RUN SCRIPT


if it doesn't find the word it'll carry on with the program.
mnutz
 
Posts: 10
Joined: Sun Nov 17, 2013 8:11 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests