Solved

Parse the unix ls command with spaces in the file name

Posted on 2004-04-26
11
1,317 Views
Last Modified: 2012-06-21
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.

0
Comment
Question by:mapons
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
 
LVL 4

Accepted Solution

by:
vi_srikanth earned 250 total points
Comment Utility
#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
 
LVL 4

Expert Comment

by:vi_srikanth
Comment Utility
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
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
mapons,
Consider this:

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

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

Stefan
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:mapons
Comment Utility
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
 
LVL 18

Expert Comment

by:kandura
Comment Utility
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
 
LVL 4

Expert Comment

by:vi_srikanth
Comment Utility
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
 
LVL 18

Expert Comment

by:kandura
Comment Utility
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
 

Author Comment

by:mapons
Comment Utility
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
 

Expert Comment

by:chip_n_rut
Comment Utility
What about using File::List
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now