sed - how to do regex groups using sed

This is what Birei and Thor mean:

sed -r "s/([a-z]*-[a-z]*-)([0-9]*-)([a-z]*-)(.*)/\1\n\2\n\3\n\4/"

Output:

test-artifact-
201251-
balbal-
0.1-SNAPSHOT.jar

You have to escape parentheses to group expressions:

\([a-z]*-[a-z]*-\)\([0-9]*-\)\([a-z]*-\)\([.]*SNAPSHOT.jar\)

And use them with \1, \2, etc.


EDIT: Also note just before SNAPSHOT that [.] will not match. Inside brackets . is literal. It should be [0-9.-]*


infact for those regular string, awk could save you from grouping. :)

you just give the part index number you want:

awk 'BEGIN{FS=OFS="-"}{print $1,$2,$5,$6}' 

output:

kent$  echo "test-artifact-201251-balbal-0.1-SNAPSHOT.jar"|awk 'BEGIN{FS="-";OFS="-"}{print $1,$2,$5,$6}'
test-artifact-0.1-SNAPSHOT.jar

Tags:

Linux

Regex

Sed