0

I have working example of substitution in online regex tester https://regex101.com/r/3FKdLL/1 and I want to use it as a substitution in sed editor.

echo "repo-2019-12-31-14-30-11.gz" | sed -r 's/^([\w-]+)-\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}.gz$.*/\1/p'

It always prints whole string: repo-2019-12-31-14-30-11.gz, but not matched group [\w-]+. I expect to get only text from group which is repo string in this example.

2
  • 1
    @NickReed Sed doesn't support \K (PCRE). Commented Jan 2, 2020 at 16:38
  • @BenjaminW. Sure enough, I tried it with -r and it doesn't work. Good catch.
    – Nick Reed
    Commented Jan 2, 2020 at 16:41

2 Answers 2

2

Try this:

echo "repo-2019-12-31-14-30-11.gz" |
sed -rn 's/^([A-Za-z]+)-[[:alnum:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}-[[:digit:]]{2}-[[:digit:]]{2}-[[:digit:]]{2}.gz.*$/\1/p'

Explanations:


Additionaly, you could refactor your regex by removing duplication:

echo "repo-2019-12-31-14-30-11.gz" |
sed -rn 's/^([[:alnum:]]+)-[[:digit:]]{4}(-[[:digit:]]{2}){5}.gz.*$/\1/p'
0
1

Looks like a job for GNU grep :

echo "repo-2019-12-31-14-30-11.gz" | grep -oP '^\K[[:alpha:]-]+'

Displays :

repo-

On this example :

echo "repo-repo-2019-12-31-14-30-11.gz" | grep -oP '^\K[[:alpha:]-]+'

Displays :

repo-repo-

Which I think is what you want because you tried with [\w-]+ on your regex.

If I'm wrong, just replace the grep command with : grep -oP '^\K\w+'

1
  • nice to know, but how remove last dash?
    – QkiZ
    Commented Jan 2, 2020 at 22:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.