Solved

running count in perl

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to strip .csv from file name 9 75
PERL variable conundrum 9 84
Perl Frameworks 1 56
Perl File::Find alternative 1 28
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

13 Experts available now in Live!

Get 1:1 Help Now