LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   grep issue of escaping backticks in perl (https://www.linuxquestions.org/questions/programming-9/grep-issue-of-escaping-backticks-in-perl-186344/)

rajatgarg 05-26-2004 11:29 PM

grep issue of escaping backticks in perl
 
Hi all,

i have a file where "lines have uh, or huh or uh-huh" in them
i want the line numbers with only "uh" in them


now the issue is:

in perl,

@abc = `grep -n uh $file`;

gives the result with all uh, huh, uh-huh

i tried reg-ex but it is not working properly, especially single quotes are giving some issues...


pls help



regards,
rajat

hallamigo 05-27-2004 01:25 AM

If 'uh' is the only thing on the line then try something like this. (I try to avoid using grep or any other 'system' command if Perl has an internal way of doing it.)

Code:

#!/usr/bin/perl -w
use strict;

my $file = '/usr/local/foo.txt';

open( FILE, $file ) || die "Cannot open $file: $!";
my @display = <FILE>;
close(FILE);

my $line_count = 1;

foreach my $line (@display) {

    chomp($line);      #Remove the newline \n
    $line =~ s/(\s)//g; #Remove any spaces

    if ($line eq 'uh') {
        print "Found uh on line $line_count";
    }

    $line_count++;

}


Tinkster 05-27-2004 01:27 AM

Not using perl here, but try:
Code:

`egrep -n 'uh[ $]+'`


Cheers,
Tink


All times are GMT -5. The time now is 10:48 PM.