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.

QkiZ
  • 798
  • 1
  • 8
  • 19

2 Answers2

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'
Amessihel
  • 5,891
  • 3
  • 16
  • 40
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+'

Corentin Limier
  • 4,946
  • 1
  • 13
  • 24