Solved

Parse the unix ls command with spaces in the file name

Posted on 2004-04-26
11
1,335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10917375
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
ID: 10917393
#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
ID: 10917404
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:stefan73
ID: 10917436
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
ID: 10917451
mapons,
Consider this:

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

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

Stefan
0
 

Author Comment

by:mapons
ID: 10917654
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
ID: 10917769
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
ID: 10917834
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
ID: 10918159
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
ID: 10918265
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
ID: 11276294
What about using File::List
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

688 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