Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An ack or grep regex to match two lines, that are nearly the same

Tags:

regex

grep

I'm looking in our code for something that looks very much like:

#$foo="bar";
$foo="baz";

Problems:

  1. I don't know the name of the variable
  2. I don't know which line is actually commented out.
  3. I really don't seem to be able to properly write a multi-line regex.

My initial attempt was ack /\$(.\*)=.\*$\$($1)=/

I prefer ack usually for code grepping, but I don't mind using grep either. JWZ probably thinks I have way more than 2 problems by now.

like image 796
Neth Avatar asked Oct 05 '10 12:10

Neth


People also ask

Can grep match multiple lines?

grep is a command line text searching utility that is able to find patterns and strings in files and other types of input. Most matches will match on one line only, but it's often useful to match across multiple new lines.

How do you do multi line grep?

Let's break this command down: The -P option enables the PCRE add-on for grep. The -z option treats the matched text as a sequence of lines by adding the NUL character to each line's ending. The -o option enables grep to print only the matched text and ignore trailing spaces.


1 Answers

Neither grep nor ack will handle more than one line at a time.

like image 190
Andy Lester Avatar answered Sep 26 '22 03:09

Andy Lester