LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-24-2009, 08:47 AM   #1
suran
LQ Newbie
 
Registered: Jan 2009
Posts: 14

Rep: Reputation: 0
manipulating text in python


hi,

how do i achieve the same results as my shell script in python:

the shell script i wrote:

Code:
#!/bin/ksh
#script:email_logs
#date:16/10/2009
#purpose: to find emails from a specific address and generate a report that is legible

#Define variables

logdir="/scratch/syslog-ng/maillogs"
email_address=$1
output="/tmp/$1.log"

function usage
{
echo "Usage: email_logs <email_address>" 
echo "email_logs sabrix@malca.custhelp.com"
}

#####check for correct usage

if [ $# -ne 1 ]

then
        usage 
        exit 1
fi

#####main program#######

cd ${logdir}

zgrep -E "$1" 10.2[0-9].0.1[0-100]/maillog.[0-99].gz | awk '{print " To: "$13,"From: "$12, "Subject: "$21,$22,$23,$24,$25, "Date & Time: " $1,$2,$3}' \
| sed 's%10.2[0-9].0.1[0-99]/maillog.[0-99].gz:%%g' >>${output} #checks on the 10.20. address range

an example of the file it generates when you give $1 a value is like so:

To: mandsmonthly@mailuk.custhelp.com From: Helen.Owen2@marks-and-spencer.com Subject: - EMPLOYEE CONTRACT CHANGES - Date & Time: Nov 18 09:59:01


bascially the to is filed number 13, from 12, subject 21-25 data and time 1-3


How do i get this exact same thing to work with python?

my python code so far:

Code:
#!/usr/bin/env python
import subprocess
import optparse
import re
import os
import gzip
import sys

LOGDIR="/scratch/syslog-ng/maillogs"
email = raw_input('Please enter email address: ')

im not really sure what to do from here as python is completely new to me.

Last edited by XavierP; 11-25-2009 at 03:23 AM. Reason: added code tags for readability
 
Old 11-25-2009, 01:30 AM   #2
ywzhaifei
LQ Newbie
 
Registered: Nov 2009
Distribution: archlinux
Posts: 6

Rep: Reputation: 0
i write the python code for below
Code:
zgrep -E "$1" 10.2[0-9].0.1[0-100]/maillog.[0-99].gz | awk '{print " To: "$13,"From: "$12, "Subject: "$21,$22,$23,$24,$25, "Date & Time: " $1,$2,$3}' \
| sed 's%10.2[0-9].0.1[0-99]/maillog.[0-99].gz:%%g'
Code:
#import re, os.path, gzip
for i in range(10):
	for j in range(101):
		for k in range(100):
			file='10.2{0}.0.1{1}/maillog.{2}.gz'.format(i,j,k)
			contents=gzip.open(os.path.join(logdir,file), 'rb').read().split('\n')
			for item in contents:
				if item.find(email):
					items=item.split()
					out='To: {0} From: {1} Subject: {2} Date & Time: {3}'.format(items[12], items[11], items[20-24].join(' '), items[0-2].join(' ')])
					print(re.sub('10.2[0-9].0.1[0-99]\/maillog.[0-99].gz', '', out))
not test.
 
Old 11-25-2009, 03:23 AM   #3
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-30-2009, 04:55 AM   #4
suran
LQ Newbie
 
Registered: Jan 2009
Posts: 14

Original Poster
Rep: Reputation: 0
hi it says there is a syntax error here:

out='To: {0} From: {1} Subject: {2} Date & Time: {3}'.format(items[12], items[11], items[20-24].join(' '), items[0-2].join(' ')])
print(re.sub('10.2[0-9].0.1[0-99]\/maillog.[0-99].gz', '', out))
? any ideas? i've tried ammending a few times
 
Old 11-30-2009, 11:33 PM   #5
ywzhaifei
LQ Newbie
 
Registered: Nov 2009
Distribution: archlinux
Posts: 6

Rep: Reputation: 0
yeah. remove the ']'
out='To: {0} From: {1} Subject: {2} Date & Time: {3}'.format(items[12], items[11], items[20-24].join(' '), items[0-2].join(' '))

sorry about that.
and next time. Can you also post the entire error message? thanks.

Last edited by ywzhaifei; 11-30-2009 at 11:43 PM.
 
  


Reply

Tags
linux, manipulating, python, text



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Manipulating text phillipseamore Linux - Newbie 6 10-05-2008 12:51 PM
A text editor written in python? will103 Programming 5 10-05-2008 06:47 AM
Manipulating Text File with awk or sed kushalkoolwal Programming 2 09-10-2008 07:35 PM
Python: Can you add text to an image in python? BrianK Programming 2 04-03-2008 03:56 PM
Editing/manipulating text files winchester169 Linux - Software 3 08-18-2005 06:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration