Solved

running count in perl

Posted on 2002-05-17
3
181 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

23 Experts available now in Live!

Get 1:1 Help Now