Parse the unix ls command with spaces in the file name

Dear All,
              I would like to parse the unix ls -lrt command where  as the file name has spaces in it.

Example
-----------
$> ls -lrt

-r--r--r--   1 pons   test       19456 Apr 15 12:23 Network Programming with perl.doc


Could you help me with a Perl answer for this.

thanks,
Pons.

maponsAsked:
Who is Participating?
 
vi_srikanthConnect With a Mentor Commented:
#Supposing that, $_ has the content of the ls -ltr :
$_='-r--r--r--   1 pons   test       19456 Apr 15 12:23 Network Programming with perl.doc';
if (m#(.*? +){8}(.*)#)
{
    print $2;
}
print "\n Or \n";  # or u can use the following one
if (m#([A-Z][a-z]{2} [0-9]+ [0-9]+:[0-9]+ )(.*)#)
{
    print $2;
}
0
 
stefan73Commented:
Hi mapons,
On the command line, you'd use "ls -l | cut -c57-", so with Perl, the best would be substr:

ls -l | perl -ne 'print substr($_,56)'

Cheers,
Stefan
0
 
vi_srikanthCommented:
I'm sorry. Even I would recommend that substr if u r going thru perl. Bcos the column width is fixed in the ls -ltr output.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
stefan73Commented:
Hmm, maybe that's not so good. If you have irregular file lengths, this will fail. Also, 56 is fine on cygwin, but 54 on Solaris :-/

If you assume that a line looks like this:
-rw-r--r--   1 csmdev4  mndev          0 Apr 26 13:24 j

You could use a regex, like:

/^[a-z-]+\s+\d+\s+\w+\s+\w+\s+\d+\w+\d+\s+\d+\:\d+\s(.*$)/

...and use $1.

But since in Unix file names can contain ALL sorts of weird characters (including escape sequences, heading spaces & such), your probably better off using opendir/readdir/closedir:

opendir(MYDIR,".") or die "Can't opendir .";
@files=readdir MYDIR;
closefir MYDIR;

...you can then use stat for each file to get the sort criteria or filter your output. This should get all sorts of file names, with the advantage of being at least as fast as an externall call of ls.
0
 
stefan73Commented:
mapons,
Consider this:

perl -e 'mkdir("a\nb\nc",0755)

...works and creates a valid, albeit highly unusual directory.

Stefan
0
 
maponsAuthor Commented:
Dear All,
               Please note that, i am trying to parse the ls -lrt command .

Example:
------------
-rw-r-----   1 pons   staff      31262 Jun 08 2001  svsmgr.zip
-r--r--r--   1 sakthi   test       19456 Apr 15 12:23 Network Programming with perl.doc

* Srikanth's will not work for the first line.
Please advise me on this.

I tried for substr also, this is not giving the proper output.


0
 
kanduraCommented:
Is there an overriding reason why you don't use a combination of readdir and stat? There are perl functions to find out stuff about files, which will lead you to a more robust program. Remember, the output of ls is intended to be human-readable, and not necessarily machine-readable.
Several exceptions to ls's format have been shown already. I would also like to add a couple:
- Long usernames, or large numeric user id's.
- very large files.

So, what information from the ls -lrt command are you interested in?
0
 
vi_srikanthCommented:
Can u plz tell me, why my code wont work for the first line. Also, I think substr should also work. Can u just tell the reason why it wont work?
0
 
kanduraCommented:
here' s two examples of ls -lrt output from my system:

# regular line. filename starts at 56
-rw-r--r--    1 root     root        16251 Mar  1 06:31 etc.2.200403010631.tar.bz2
# large file pushes filename to 58
-rw-r--r--    1 root     root     2314173410 Mar  1 17:26 10.0.0.10-cygdrive-d-data-www.0.200403011356.tar.bz2
# large numeric uid: filename starts at 62
drwx------    2 4294967294 4294967294       64 Apr 19 15:22 fileserver

Note that EE ruins the spaces again. Is there still no <code> tag for us?


vi_srikanth: The first line of the ls -lrt output says: "total 14245", which doesn't match your regex. Of course, mapons should just discard that line before starting to parse. Substr doesn't work because of the exceptions I gave above, and the other exceptions mentioned in this thread.

So as long as we don't know what the exact information is that mapons is looking for, I would recommend readdir/stat. I just noticed that stephan73 also recommended this.
0
 
maponsAuthor Commented:
Srikanth,
    Your code will work with slight modifications[ Second reg expr ]

if ($a =~ m#(.*? +){8}(.*)#)
{
    print " File >> $2 \n";   ## Working fine for me..
}

if($a =~ m#([A-Z][a-z]{2} [0-9]+ (([0-9]+:[0-9]+)|([0-9]+)))(.*)#)
{
    print " File Name : $5 \n";
}



         

0
 
chip_n_rutCommented:
What about using File::List
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.