LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2004, 11:29 PM   #1
rajatgarg
Member
 
Registered: Oct 2003
Posts: 41

Rep: Reputation: 15
Unhappy 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
 
Old 05-27-2004, 01:25 AM   #2
hallamigo
Member
 
Registered: Feb 2004
Location: Utah, USA
Distribution: Debian
Posts: 230

Rep: Reputation: 31
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++;

}

Last edited by hallamigo; 05-27-2004 at 01:30 AM.
 
Old 05-27-2004, 01:27 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Not using perl here, but try:
Code:
 `egrep -n 'uh[ $]+'`


Cheers,
Tink
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl not running backticks within chroot jail Consul Linux - Security 2 08-02-2005 05:36 PM
Need to assign result of grep to var in PERL amytys Programming 1 09-23-2004 06:25 PM
howto grep in perl bishal Programming 7 07-19-2004 01:32 AM
PERL: problem with grep ocularbob Programming 9 05-01-2003 05:36 PM
Need help with grep or perl FredrikN Programming 2 12-16-2001 11:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration