57

Is there anyway you can do regex match group using sed like java regex pattern/match/group?

if i have string like

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

how do I use sed just to get the result like:

test-artifact-0.1-SNASHOT.jar

I am wondering does sed allow you to do something like java regex, you define the pattern like:

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

and then you can get the results as an array like:

test-artifact-
201251-
balbal-
0.1-SNAPSHOT.jar
Shengjie
  • 12,336
  • 29
  • 98
  • 139

4 Answers4

79

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.-]*

Birei
  • 35,723
  • 2
  • 77
  • 82
  • 25
    With GNU `sed` you can avoid all the escaped parenthesis by using extended regular expressions. Use the `-r` switch to do this. – Thor Jul 25 '12 at 13:59
  • 4
    `-E` also works in GNU to not need to escape parens. – abalter Oct 14 '16 at 17:11
24

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
Steve
  • 51,466
  • 13
  • 89
  • 103
7

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
Kent
  • 189,393
  • 32
  • 233
  • 301
3

If you are searching for an easier way I guess this might be of your help! :)

echo "est-artifact-201251-balbal-0.1-SNAPSHOT.jar" | cut -d- -f1,2,5,6

"-" used as delimeter and fields 1,2,5,6 are printed.

Note: This would require you to know the exact position of the field.

SAM80DEV
  • 31
  • 1