Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

running count in perl

Posted on 2002-05-17
3
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 total points
ID: 7022758
Has this helped you?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
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…
Six Sigma Control Plans

670 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