Solved

running count in perl

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 185
perl to display a matrix report for 3-dimenional array 3 71
Add additional column to .csv using Perl. 8 155
Perl Frameworks 1 98
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

821 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