I want to grep a link from an external file example.txt
.
example.txt
containins:
(https://example.com/pathto/music.mp3)music.mp3
the code:
egrep -o -m1 '(https)[^'\"]+.mp3' example.txt
output:
https://example.com/pathto/music1.mp3)music.mp3
When I run grep, it detect the last .mp3 as end of output while I just need it end after first occurrence. How can I tell grep to stop after finding the first pattern?
My desired output:
https://example.com/pathto/music.mp3
I just want to extract any string starting with https
and ending with mp3
Best Answer
Standard grep does not accept the
?
modifier that would normally make it non-greedy.But you can try the -P option that - if enabled in your distro - will make it accept Perl style regexes:
If that does not work, you could for your specific example include the right parenthesis in the range so it wouldn't look beyond the parenthesis: