![grep regexp grep regexp](https://www.cyberciti.biz/media/new/faq/2010/02/Linux-and-Unix-Regular-Expressions-In-grep.png)
You simply replace one modular utility with another and tweak the parameters. With this solution there is a bare minimum of change required in the script. The parameter -o works just like the grep version if it is bare, but it also accepts a numeric parameter in pcregrep, which indicates which capturing group you want to show. The pcregrep tool utilizes all of the same syntax you've already used with grep, but implements the functionality that you need. To get only the contents of the capturing group 1. To the following: name=$(echo $f | pcregrep -o1 -Ei '+_(+)_*') Try changing the lines: echo $f | grep -oEi '+_(+)_*' I realize that an answer was already accepted for this, but from a "strictly *nix purist angle" it seems like the right tool for the job is pcregrep, which doesn't seem to have been mentioned yet.
![grep regexp grep regexp](https://cdn.webservertalk.com/wp-content/uploads/p1-2-656x131.png)
This means that if you use ack -w foo:, the new ack will not. Now, ack disallows regexes that begin or end with non-word characters. ack would make guesses as to what your intent was, but it was not well-defined. Unix philosophy is to have tools which do one thing, and do it well, and combine them to achieve non-trivial tasks, so I'd argue that grep + sed etc is a more Unixy way of doing things :-) The -w option, which tells ack to only find whole-word matches, did not always work if your pattern began or ended with puncutation. This gets cut to parse the line into fields by splitting on the delimiter _, and returns just field 2 (field numbers start at 1)). If you're using Bash, you don't even have to use grep: files="*.jpg"įor f in $files # unquoted in order to allow the glob to expand