Solved

Parse the unix ls command with spaces in the file name

Posted on 2004-04-26
11
1,324 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Uploading Binary Data using Perl 5 90
Perl count the hash for print 4 166
Perl modules on linux ec2 3 104
Perl Frameworks 1 90
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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.…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

809 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