regexp question :

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zcabeli
    New Member
    • Jan 2008
    • 51

    #1

    regexp question :

    Hello,

    i'd like to detect all the lines that their first alpha-numeric word repeats more then once.

    for example: this line would be 'hits'
    abc sda asd asd abc
    aaa bbb ccc aaa aaa


    thanks,
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Check out the perldoc's. You could also shows us what you tried.

    --Kevin

    Comment

    • zcabeli
      New Member
      • Jan 2008
      • 51

      #3
      unfortunately i only find how to do it in more than one code line.
      meaning that at first i extract the first word, and then i make the replacements.

      $string =~ m/(\w+)/;
      $string =~ s/$1/$replace_string/;

      i don't know how to count the number of occurrence of the first line. so i'll hit only the lines that their first word repeats itself at some point.

      i also believe that there is more elegant way to do it.

      thanks,

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Originally posted by zcabeli

        i don't know how to count the number of occurrence of the first line.
        thanks,
        You want to count number of occurences of first line or occurence of first word in each line?
        If you want to count occurences of first word and to display only the hits, you can use
        Code:
        open(IN,"input.txt") or die "sorry:$!";
        while(<IN>)
        {
           /^(\w+)/;           ## same as $_ =~/^(\w+)/;
          my $str=$1;
          my $c=s/$str/$str/g;   ## similar to $c=$_=~s/$str/$str/g;
          print "{count=$c}:$_ " if($c>1);
        
        }

        Comment

        Working...