LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-09-2005, 08:27 PM   #1
xgreen
Member
 
Registered: Aug 2003
Distribution: Slackware,Arch
Posts: 389

Rep: Reputation: 30
Python: filtering table


i've a file contain a 'ls -l' output. i need to filter out all text file (*.txt)

e.g of ls -l output in file named "test":

code :
--------
y=file("/home/nazmin/temp/test","r")

for i in y:
lines=i.strip()
print lines
y.close()

output
--------
-rw-r--r-- 1 nazmin users 25K 2005-05-27 21:59 linux_serial.txt
drwx------ 2 nazmin users 112 2005-06-08 20:57 mail/
-rw-r--r-- 1 nazmin users 258 2005-06-05 01:50 metallica
-rw-r--r-- 1 nazmin users 5.0K 2005-05-21 10:18 msg00123.html

output required :

linux_serial.txt
 
Old 07-10-2005, 12:58 AM   #2
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
This could be tricky, but if you can assume that the user, group , and file names don't have spaces in them, you can split each line on the spaces and take the 8th field. If the last 4 characters are ".txt" you could print it.

Example:
Code:
filename = line.split(' ')[7]
if filename[-4:] == ".txt":
    print filename
If the fields themselves can have spaces in them, then the problem becomes trickier.

One thing that strinkes me as odd, though-- why not just use "ls *.txt"?
 
Old 07-10-2005, 01:46 AM   #3
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Why not just split on the colon in the time field? I'm pretty sure a colon wont be showing up anywhere before that point:
Code:
files = [ x.strip().split(':',1)[1][3:] for x in lines if x.strip()[-4:] == '.txt' ]
 
Old 07-10-2005, 05:38 AM   #4
xgreen
Member
 
Registered: Aug 2003
Distribution: Slackware,Arch
Posts: 389

Original Poster
Rep: Reputation: 30
thanks guys... i've tired your suggestion..but no luck but i managed to write the code (i think the code quite long for the job ...need to find better ways of doing it :-) )



Code:
y=file("/home/nazmin/testing/test","r")
for x in y:
        lines=x.strip()
        list=[]
        for i in lines:
                  list.append(i)

       c=list.index(":")
       new_list=list[c+4:]
	
      txt=new_list[-4:]
      new_txt="".join(txt)
      if new_txt == '.txt':
                new_word="".join(new_list)
                print new_word	
y.close()

Last edited by xgreen; 07-10-2005 at 05:41 AM.
 
Old 07-10-2005, 04:07 PM   #5
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
That's exactly what my line does, except in a much more convoluted way. The only difference is, my line doesn't actually produce the output, it makes a list of the filenames. So you could do this:
Code:
y=file("/home/nazmin/testing/test","r")
files = [ x.strip().split(':',1)[1][3:] for x in y if x.strip()[-4:] == '.txt' ]
for f in files:
    print f
 
Old 07-11-2005, 06:19 AM   #6
xgreen
Member
 
Registered: Aug 2003
Distribution: Slackware,Arch
Posts: 389

Original Poster
Rep: Reputation: 30
CroMagnon, thanks for the code & it is working. could you explain the meaning of this code.

files = [ x.strip().split(':',1)[1][3:] for x in y if x.strip()[-4:] == '.txt' ]
 
Old 07-11-2005, 03:50 PM   #7
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Sure. It might be easier to explain in smaller chunks. Excuse me if I tell you stuff you already know...

Code:
[x for x in range(10)]
This builds a list by walking through the list provided by range(10), effectively making a list like this: [0,1,2,3,4,5,6,7,8,9]

Not very useful by itself But then we can add a condition, like this:
Code:
[x for x in range(10) if x < 5]
This walks through the list and only includes the value if it meets the condition, so we get [0,1,2,3,4].
Finally, we can modify the value before it is added to the list, like this:
Code:
[x * 2 for x in range(10) if x < 5]
So it takes each value that meets the condition, and multiplies it by 2, so we get [0,2,4,6,8].

So, on to my code:
Code:
files = [ x.strip().split(':',1)[1][3:] for x in y if x.strip()[-4:] == '.txt' ]
x.strip() - removes the spaces
.split(':',1) - breaks the string into two pieces, around the colon (the 1 means to only split once, not for every colon, just in case)
Because the result of split is a list, and we want the data after the colon, I add [1] to get the right list element, then we use [3:] to chop the first 3 characters off the start of the string.
The condition at the end just stops the program from operating on any line that doesn't end with ".txt".

Hope that helps, let me know if you have any questions.
 
Old 07-13-2005, 06:07 AM   #8
xgreen
Member
 
Registered: Aug 2003
Distribution: Slackware,Arch
Posts: 389

Original Poster
Rep: Reputation: 30
CroMagnon,

thanks for the superb explanation...really helping
 
  


Reply



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
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Fedora 2 04-23-2009 07:02 PM
installing python library's (Python Numeric) Four Linux - Newbie 1 10-16-2005 02:31 PM
MySQL non-realtime table-by-table mirroring Passive Linux - Software 1 01-20-2004 12:11 PM
How to import MS ACCESS Table including OLE filed into the MySQL Table ? myunicom Linux - General 1 11-28-2003 11:30 AM
Sendmail Spam filtering and Virus filtering MrJoshua Linux - General 2 04-03-2003 10:12 AM

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

All times are GMT -5. The time now is 04:46 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