1

I have a text with a list of paths and I'd like only select one specific path to a yml inside all directories with a specific prefix (foo).

So when I run grep -Eo "^config/foo.*/db.yml", it also selects other db.yml files in subdirectories and that's not what I'm expecting :(

Actual output:

config/footestto/db.yml
config/foodummy/db.yml
config/footestto/prod/db.yml

Expected output:

config/footestto/db.yml
config/foodummy/db.yml

Could you please help me? there could be something wrong with my regex. Thanks.

Aziz
  • 931
  • 2
  • 10
  • 19
  • 1
    use `[^/]*` instead of `.*` (there's a duplicate somewhere like this one whose answer fits but the question is different: https://stackoverflow.com/questions/1103149/non-greedy-reluctant-regex-matching-in-sed) – Sundeep Sep 16 '21 at 07:15
  • 1
    If the output is from the `find` command, you can explore its options `-mindepth` and `-maxdepth`, which can be set appropriately to only print the files you need, at the needed directory tree depth/level. – Timur Shtatland Sep 16 '21 at 13:53
  • You show expected output but not the input you'd get that output from so that's only half an example and leaves us guessing. [edit] your question to include the sample input. – Ed Morton Sep 18 '21 at 12:16

1 Answers1

0
$ egrep -w '^config\/foo[a-z]+\/db.yml' test.txt

Your regex is incorrect:

\/ matches the character /

[a-z]+ matches letters one and unlimited times

until it founds /db.yml

Konflex
  • 465
  • 3
  • 11
  • 1
    `egrep` is deprecated in favor of `grep -E`, the character class `[[:lower:]]` is more portable than `[a-z]` but `[^/]` would be more accurate anyway, the OP was using `.*` so you should have `*` instead of `+` so as not to require 1 or more extra chars, the `.` is a wildcard so `db.yml` would match `dbxyml`, the expression isn't anchored at the end and so would match `.../dbxymlfoo.txt`, you don't need to escape `/` in a regexp as it's already literal, and the `-w` is to match words but your regexp contains non-word-constituent characters. So ITYM `grep -E '^config/foo[^/]*/db\.yml$'`. – Ed Morton Sep 18 '21 at 12:25
  • Thanks for your answer, i can improve my regex knowknowlegde also, i test it and i think you forget the ```\``` so the good one should be ```grep -E '^config\/foo[^\/]*\/db\.yml$' ``` – Konflex Sep 19 '21 at 08:01
  • You're welcome but as I said in my previous comment - you don't need to escape `/` in a regexp as it's already literal so `/` is correct, you don't need to do `\/`. Looking at it again, you don't need `-E` either as there's nothing in that regexp that requires EREs so the command can just be `grep '^config/foo[^/]*/db\.yml$'`. – Ed Morton Sep 19 '21 at 11:55