Solved

running count in perl

Posted on 2002-05-17
3
183 Views
Last Modified: 2011-10-03
I need to go through a list of files and count how many symbolic (soft) links there are to each file (if any).

I am currently doing this to find symlinks

#!/usr/bin/perl -w
$symcount=0;

while (<>) {

if (/->/) {
     print $_;
$symcount++;

     }

     }

     print "there are $symcount symbolic links\n";

which gives me
=========================================================
lrwxrwxrwx   1 cyr xy80           4 May 16 20:28 test -> test
lrwxrwxrwx   1 cyr xy80           7 May 16 21:10 test333 -> test333
there are 2 symbolic likks
=========================================================
I now want to keep a count of files that have multiple symlinks to them.  I am only interested in files that are listed in the input file (i.e. I dont need to chase files acorss the file system)

please help!!
0
Comment
Question by:cyrobinson
  • 2
3 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 7017985
To simplify things, I've added the file entries to a DATA block, to simulate your file of file info. This will count the multiple links, not sure if you want to also display the other attributes of that file

while (<DATA>) {
   if ( /.*\->\s+(\w+)$/ ) {
      $filename = $1;
      $sym_links{$filename}++;
   }
}
close(DATA);

foreach $file (keys %sym_links )
{
   if ( $sym_links{$file} > 1 )
   {
      print("File $file has $sym_links{$file} symlinks to it\n");
   }
}


__DATA__
lrwxrwxrwx   1 cyr xy80           4 May 16 20:28 test -> test1
lrwxrwxrwx   1 cyr xy80           7 May 16 21:10 test333 -> test333
lrwxrwxrwx   1 cyr xy80           4 May 16 20:28 test1a -> test1
lrwxrwxrwx   1 cyr xy80           7 May 16 21:10 test4 -> test4
0
 
LVL 84

Expert Comment

by:ozo
ID: 7017988
while (<>) {
    if( /->\s*(.*)/ ){
        $symcount{$1}++;
        print $_;
        $symcount++;
    }
}
print "there are $symcount symbolic links\n";
while( ($k,$v) = each %symcount ){
    if( $v >= 2 ){
        print "$k has multiple symlinks\n";
    }
}
0
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 500 total points
ID: 7022758
Has this helped you?
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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.

770 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