how to make a script for this case?

Bengt Richter bokr at oz.net
Fri Aug 16 00:38:26 EDT 2002


On 15 Aug 2002 10:12:46 -0700, hhhnnn_hk99 at yahoo.com.hk (D99) wrote:

>hi
>
>i have a question 
>I am using linux platform and have a file which contain the 
>following data 
>
>#####################################################
>a:admin99
>b:admin at company.com
>password:123321
>e:456789
>
>b:peter at company.com
>e:1234155
>password:44587
>a:peter123
>
>
>b:tom at company.com
>a:tom22
>password:4sfsfsf7
>e:1sfsfs4155
>
>
>#####################################################
>and i want to run a shell script to search and sort to as
>below file
>
>account    password       alais        
>######################################################
>admin99     123321       admin
>peter123    44587        peter
>tom22       4sfsfsf7     tom
>
>########################################################
>
>would anyone can help me ? thanks a lot

I am using version 2.2. This is first hack:

--< h3n3hk.py >---------------------------------
import sys
def h3n3hk(f=sys.stdin):
    d = {}; out = []
    while 1:
        line = f.readline()
        if line.strip()=='':
            if line and not d: continue
            out.append('%-12s%-12s%-12s' % (
                d.get('a','??'),
                d.get('password','??'),
                d.get('b','??').split('@')[0]
            ))
            if line == '': break
            d={}
        else:
            ls = line.split(':', 1)
            if len(ls)==2 and ls[0] in ['a', 'b', 'password']:
                d[ls[0]] = ls[1].strip()
    out.sort()
    out.insert(0,'%-12s%-12s%-12s' % ('account','password','alias'))
    out.insert(1,'%-12s%-12s%-12s' % ('-------','--------','-----'))
    for line in out:
        print line

if __name__=='__main__':
    import sys
    try:
        h3n3hk(file(sys.argv[1]))
    except IOError, e:
        print e
    except:
        print 'Usage: h3n3hk.py file'
--<EOF>-----------------------------------------

[21:41] C:\pywk\junk>type h3n3hk.txt
a:admin99
b:admin at company.com
password:123321
e:456789

b:peter at company.com
e:1234155
password:44587
a:peter123


b:tom at company.com
a:tom22
password:4sfsfsf7
e:1sfsfs4155

[21:42] C:\pywk\junk>h3n3hk.py
Usage: h3n3hk.py file

[21:42] C:\pywk\junk>h3n3hk.py h3n3hk.txt
account     password    alias
-------     --------    -----
admin99     123321      admin
peter123    44587       peter
tom22       4sfsfsf7    tom

Regards,
Bengt Richter



More information about the Python-list mailing list