4

I am trying to write a grep which finds the .epl file names or file paths in a given file.

I have tried the following regexs

Part 1 : REGEX Used grep -rEco "/(.+).epl|(.+).epl" ./index.epl Result obtained

url => 'xyz.epl
url => 'xyz.epl
url => 'xyz.epl
url => "packet/xyz.epl
url => "/web/abc/xyz.epl
url => qq{/web/abc/xyz.epl
url => '/web/abc/def/xyz.epl
url => '/web/abc/def/xyz.epl
url => 'xyz.epl
url => 'xyz.epl
$url = "/web/abc/def/xyz.epl
var requestURL = "xyz.epl
var requestURL = "xyz.epl
<a style="float:left;" href="xyz.epl
[- Execute('components/xyz.epl
Welcome, <a href="xyz.epl
[- Execute('components/xyz.epl
<a href="xyz.epl
[- Execute(`'components/xyz.epl`

However I want it to match only the following (with or without the preceding quotes)

'xyz.epl(OR xyz.epl)
'xyz.epl
'xyz.epl
"packet/xyz.epl(OR packet/xyz.epl)
"/web/abc/xyz.epl(OR "/web/abc/xyz.epl)
/web/abc/xyz.epl
'/web/abc/def/xyz.epl
'/web/abc/def/xyz.epl
'xyz.epl
'xyz.epl
"/web/abc/def/xyz.epl
"xyz.epl
"xyz.epl
"xyz.epl
'components/xyz.epl
"xyz.epl
'components/xyz.epl
"xyz.epl
'components/xyz.epl

To obtain the desired out I tried grep -rEoc '\/(.+)\.epl|[\"\'](.+)\.epl' However it gives me an "unmatched '." error.

I removed the quotes surrounding the regex and I got "Badly placed ()'s."

1

Given your sample input, this produced your desired output:

grep -Eo '["'\'']?[/[:alnum:]]+\.epl'

It makes some assumptions about the characters in your filenames (letters, numbers, slashes) -- feel free to throw more characters into that set of brackets.

1

I think this will do it for you:

grep -oP "(?<=['\"]|qq{)[xp/c][^\`]+" ./index.epl

This says:

-o Only keep the matching part.
-P Use Perl style regex (we need this for the lookbehind)
"..." Use double quotes since you can't escape the single within single
(?<=...) Start a positive lookbehind group. This means that whatever we match much also have this pattern right before it, but the "before part" isn't part of the pattern we want to keep.
[\'\"] Match one of: single or double quote
|qq{ OR match the literal string 'qq{'
Once you find that pattern match:
[xp/c] Match one of x, p, /, or c. (This is not so nice but you have a small number of cases to check.)
[^]+` Anything that's not a backtick, 1 or more times

EDIT: If you want the single or double quotes to be part of the pattern it will be a little more tricky since you don't seem to want the qq{ to be included either way.

  • You are correct. The escape was a typo. I don't know a good way to get the last quote (i tried look ahead but couldn't get it to work). The single matching char after the ' or " will do it but it's not very regexy. Do you have a suggestion I'm missing? – user1794469 May 25 '16 at 17:36
  • Not any good suggestions at the moment. Have only glimpsed at it and doubt I'll look at it now as it is "answered" :P – Runium May 26 '16 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.