Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep-like with Perl 1

Status
Not open for further replies.

Tiamose

Programmer
Nov 13, 2007
10
VN
Hi all,

Does anyone know a bout a funtion which is similar to grep in Unix? I want to count the number of lines which match a pattern in a file and I intend to use "grep" command of Unix system:
$n=`grep -c -i "$line" filename`;

However if $line contains double quotation marks (i.e. " ) or single quotation mark (i.e. ' ) then the program doesn's work well. (However, there's no problem If I use directly this command in Unix)


Thank you very much,
Tiamose.

 
@out = grep /blah/i, @array;
$count = @out;
print $count;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Perl has it's own grep function. It works differently than the unix grep command but is very similar and you can use it for what you're trying to do.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi,

I used grep command as trav69 suggested (@out = grep /$line/i, @array;), but I got this error

Nested quantifiers in regex; marked by <-- HERE in m/'The Working Papers for the ANSI-X3J16/ISO-SC22-WG21 C++ <-- HERE Standards Committee'/

Maybe in the $line contains some special characters which cause s this error. How can I fix it?

Thank you,
Tiamo.
 
In perl, you should always use the block form of grep. It's cleaner, more consistent, and helps enforce the difference between it and the unix grep.

In this instance if you're going to be matching on the value of a string literal "$line", then you need to escape it using quotemeta.

Code:
[blue]@out[/blue] = [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red][red]/[/red][purple][purple][b]\Q[/b][/purple][blue]$line[/blue][purple][b]\E[/b][/purple][/purple][red]/[/red][red]i[/red][red]}[/red] [blue]@array[/blue][red];[/red]

- Miller
 
Thank you MillerH, could you explain me what \Q and \E mean in your command?
 
\Q is the string interpolation alias for quotemeta. It escapes all non-word characters after it in an double quoted string AFTER the interpolation step. Therefore ensuring that the value of $line is treated as a literal string.

\E indicates that \Q should stop. Technically it's not needed in your regex since it's at the end anyway. But for consistency I always include it, since it's a way of explicitly documenting that you truly mean to escape just the value of $line.

You can read more about interpolation of double quoted strings here:
[ol]
[li]perldoc perlop - Quote and Quote-like Operators[/li]
[li]perldoc perlop - Gory details of parsing quoted constructs.[/li]
[/ol]

- Miller

PS
Not escaping special characters is the number one problem for users posting questions about regular expressions in this forum.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top