1

I am trying to use a single grep command to search for all instances of $ until a single =

I have the following command so far grep -E '\$.*=' which returns all lines with a $ and then a = but it also returns lines with == which I dont want. I want it to match the lines below

$string = 'content';    (match)
$_array['key']=12;      (match)

if ($str == 'pass')     (no match)
return $var1==$var2;    (no match)

I get my intended results with this command grep -E '\$.*=' | grep -v '==' but I want to make it work with one grep command and no piping

4
  • 3
    '\$[^=]*=[^=]*$'? Commented Nov 2 at 15:39
  • 1
    @Cyrus $a = ("v"==$b); ? Commented Nov 3 at 6:59
  • 1
    grep -E '^[^=]*(=[^=]+)*\$[^=]*=([^=]+(=[^=]+)*)?$' Demo I'd explain it but your question was closed for some totaly unknown / bizzarro / obscure reason.. Voting to Reopen ! Commented Nov 3 at 19:42
  • What about $a='b'; $c='d'? Commented Nov 4 at 16:22

1 Answer 1

1
$ grep -E '\$(.*[^=])?=([^=]|$)' file
$string = 'content';    (match)
$_array['key']=12;      (match)

It looks for the $ followed by either any chars then not-= (.*[^=]) or no chars (?), then =, then not-= or the end of the line ([^=]|$).

Sign up to request clarification or add additional context in comments.

2 Comments

grep -E '\$[^=]+=([^=]|$)' is a few chars more succinct but not different enough for its own answer.
@stevesliva funny, I thought I'd made sure mine didn't match $a = ("v"==$b); as yours an @Cyrus's would, but now I find mine also matches it. Ah well, it wasnt clear if that was desirable or not and the OP seems to have left the building anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.