Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

prevent sed from include text beyond capture group

I want to use a sed capture group to get the value of an option from a list of command line arguments. What would the sed string be to be able to get the value settings.conf associated with the -f flag for the string below:

./make-certs.sh client -s int -f settings.conf -o certs/int_env

I have tried sed -E s:.* -f (.*) .*:/1: but this sed string, for this example, would give settings.conf -0. I realise the problem is .* is greedy but how would I make this non-greedy using sed?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

With GNU sed, an extended regular expression (-E) and with a backreference:

sed -E 's/.*-f ([^ ]+).*/\1/' file

Output:

settings.conf

See: The Stack Overflow Regular Expressions FAQ

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading