Answer by John Sherwood for Capturing Groups From a Grep RegEx
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. Try...
View ArticleAnswer by opsb for Capturing Groups From a Grep RegEx
This is a solution that uses gawk. It's something I find I need to use often so I created a function for it function regex1 { gawk 'match($0,/'$1'/, ary) {print ary['${2:-'1'}']}'; } to use just do $...
View ArticleAnswer by ghostdog74 for Capturing Groups From a Grep RegEx
if you have bash, you can use extended globbing shopt -s extglob shopt -s nullglob shopt -s nocaseglob for file in +([0-9])_+([a-z])_+([a-z0-9]).jpg do IFS="_" set -- $file echo "This is your captured...
View ArticleAnswer by Dennis Williamson for Capturing Groups From a Grep RegEx
If you're using Bash, you don't even have to use grep: files="*.jpg" regex="[0-9]+_([a-z]+)_[0-9a-z]*" for f in $files # unquoted in order to allow the glob to expand do if [[ $f =~ $regex ]] then...
View ArticleAnswer by RobM for Capturing Groups From a Grep RegEx
This isn't really possible with pure grep, at least not generally. But if your pattern is suitable, you may be able to use grep multiple times within a pipeline to first reduce your line to a known...
View ArticleAnswer by martin clayton for Capturing Groups From a Grep RegEx
A suggestion for you - you can use parameter expansion to remove the part of the name from the last underscore onwards, and similarly at the start: f=001_abc_0za.jpg work=${f%_*} name=${work#*_} Then...
View ArticleAnswer by cobbal for Capturing Groups From a Grep RegEx
Not possible in just grep I believe for sed: name=`echo $f | sed -E 's/([0-9]+_([a-z]+)_[0-9a-z]*)|.*/\2/'` I'll take a stab at the bonus though: echo "$name.jpg"
View ArticleCapturing Groups From a Grep RegEx
I've got this little script in sh (Mac OSX 10.6) to look through an array of files. Google has stopped being helpful at this point: files="*.jpg" for f in $files do echo $f | grep -oEi...
View ArticleAnswer by chirag nayak for Capturing Groups From a Grep RegEx
str="1w 2d 1h"regex="([0-9])w ([0-9])d ([0-9])h"if [[ $str =~ $regex ]]then week="${BASH_REMATCH[1]}" day="${BASH_REMATCH[2]}" hour="${BASH_REMATCH[3]}" echo $week --- $day ---- $hourfioutput:1 --- 2...
View ArticleAnswer by towith for Capturing Groups From a Grep RegEx
I prefer the one line python or perl command, both often included in major linux disdributionecho $'<a href="http://stackoverflow.com"></a><a href="http://google.com"></a>' |...
View ArticleAnswer by Stephen Quan for Capturing Groups From a Grep RegEx
I have been having great success at using regex with capture groups with perl, e.g.for f in 123_abc_123.jpg 123_xyz_432.jpgdo echo $f echo $f | perl -ne 'if (/[0-9]+_([[a-z]+)_[0-9a-z]*/) { print $1 ....
View Article