Issue
I am trying to figure out how to match everything between the time and the colon (the colon should also be removed) with sed. The time can be different (24H format) and should not be limited to 21:30 which is the example here.
I am trying to delete the UNWANTED TEXT:
#EXTINF:-1,21:30 UNWANTED TEXT: Apples - Oranges
The output wanted:
#EXTINF:-1,21:30 Apples - Oranges
Can anyone help me with this?
I tried following, this works however it is based on whitespace always being there and I want to use the time as the pattern.
sed 's# .*:##g'
Thanks!
Solution
No claims to elegance, but how about:
sed 's/\(.*[0-9]\{2\}:[0-9]\{2\}\)[^:]*:\(.*\)/\1\2/'
This requires the time to be two digits, colon, two digits, and will likely fail if that pattern occurs more than once in the string.
Breaking down the regular expression \(.*[0-9]\{2\}:[0-9]\{2\}\)[^:]*:\(.*\)
:
\( - start a grouping
.* - match any character until we find the next pattern, [0-9]\{2\}:[0-9]\{2\}
[0-9] - match any digit
[0-9]\{2\} - match any two digits
[0-9]\{2\}:[0-9]\{2\} - match ab:cd, where a,b,c,d are single decimal digits
\) - end the first grouping. We'll later refer to all the characters matched by this grouping as "\1"
[^:]* - match any character that's not a colon
[^:]*: - match any number of non-colon characters followed by a single colon. This is what we'll remove from the string.
\( - start the second grouping
\(.*\) - match any characters in the second grouping, which we'll refer to later as "\2"
Once we've done that match, we've more or less split our string into three pieces:
- The first grouping, called "\1", which is everything from the start of the string to the time
- The part of the string we're going to remove, which immediately follows the time and consists of a number of non-colon characters followed by a ":"
- The second grouping, which is everything after the colon.
Basically, we replace these three pieces with the first grouping and the second grouping. That's accomplished by the /\1\2/
part of the sed command.
Sed regex reference: https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html
Answered By - DraftyHat Answer Checked By - Pedro (WPSolving Volunteer)