Find A Line Of Text With A Pattern Using Windows Command Line Or Python
Solution 1:
You don't need to use Python to do this. If you're using a Unix environment, you can use fgrep
right from the command-line and redirect the output to another file.
fgrep "The signature is timestamped: "input.txt > output.txt
On Windows you can use:
find"The signature is timestamped: " < input.txt > output.txt
Solution 2:
You mention the command line utility "displays" some information, so it may well be printing to stdout
, so one way is to run the utility within Python, and capture the output.
import subprocess
# Try with some basic commands here maybe...
file_info = subprocess.check_output(['your_command_name', 'input_file'])
for line in file_info.splitlines():
# print line here to see what you getif file_info.startswith('The signature is timestamped: '):
print line # do something here
This should fit in nicely with the "use python to download and locate" - so that can use urllib.urlretrieve to download (possibly with a temporary name), then run the command line util on the temp file to get the details, then the smtplib to send emails...
Solution 3:
In python you can do something like this:
timestamp=''withopen('./filename', 'r') as f:
timestamp= [line for line in f.readlines() if 'The signature is timestamped: 'in line]
I haven't tested this but I think it'd work. Not sure if there's a better solution.
Solution 4:
I'm not too sure about the exact syntax of this exported file you have, but python's readlines()
function might be helpful for this.
h=open(pathname,'r') #opens the file for readingfor line in h.readlines():
print line#this will print out the contents of each line of the text file
If the text file has the same format every time, the rest is easy; if it is not, you could do something like
for line in h.readlines():
if line.split()[3] == 'timestamped':
print line
output_string=line
as for writing to a file, you'll want to open the file for writing, h=open(name, "w")
, then use h.write(output_string)
to write it to a text file
Post a Comment for "Find A Line Of Text With A Pattern Using Windows Command Line Or Python"