Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting string before pattern with sed (bash)

Tags:

regex

bash

sed

I need some help with sed to remove everything after matching pattern and remove the last "." if it exists..

Take this string as an example:

The.100.S02E05.720p.HDTV.x264-KILLERS.mkv

I want everything before the pattern "S[0-9][0-9]E[0-9[0-9]" except the last "."

What I want:

"The.100"

Does anyone have a great oneliner for this one?

like image 893
inz Avatar asked May 16 '26 12:05

inz


1 Answers

It sounds like you can pretty much use exactly what you had in your question:

sed 's/\.*S[0-9][0-9]E[0-9][0-9].*//'

This matches an optional . character followed by the pattern you suggested (and anything after it), replacing with nothing. You were missing a ] in the question, which I have added.

Testing it out:

$ sed 's/\.*S[0-9][0-9]E[0-9][0-9].*//' <<<'The.100.S02E05.720p.HDTV.x264-KILLERS.mkv'
The.100
like image 79
Tom Fenech Avatar answered May 19 '26 00:05

Tom Fenech