Reputation: 13
I am trying to batch edit a multiple line M3U file with sed with content like the following:
#EXTINF:-1 tvg-id="NBC.us" tvg-name="NBC US" tvg-logo="http://example.com/NBC.us.png" tvg-chno="12" group-title="United States",NBC
I'd like to find/replace so that I end up with something like the following:
#EXTINF:-1 tvg-id="NBC.us" tvg-name="NBC" tvg-logo="http://example.com/NBC.us.png" channelid="12" group-title="United States",NBC
In other words, I need to find/replace tvg-chno
to channelid
AND copy the string from (and not including) the last ,
to the end of the line and use it to overwrite the contents of tvg-name
. I have a sed command working for the first bit and a broken one not quite working as intended for the second as follows:
sed -i 's/tvg-chno/channel-id/g' test.m3u
sed -i 's/\(.*tvg-name="\)\(.*"\)\(.*",\)\(.*\)/\1\4\3\4/g' test.m3u
The second sed command outputs:
#EXTINF:-1 tvg-id="NBC.us" tvg-name="NBCUnited States",NBC
I was hoping to be able to get all the transforms done in a one-liner if possible.
Thanks for any assistance!
Upvotes: 1
Views: 288
Reputation: 58528
This might work for you (GNU sed):
sed -r 's/(tvg-name=")[^"]*(".*)tvg-chno(.*,([^,]*))$/\1\4\2channelid\3/' file
Pattern match on the strings and use back references to achieve the required result.
N.B. this assumes thattvg-chno
will always follow tvg-name
, if not, two substitutions will be needed.
Upvotes: 0
Reputation: 99154
To avoid grabbing too much as the old value, the second command should be:
's/\(tvg-name="\)\([^"]*\)\(".*",\)\(.*\)/\1\4\3\4/'
To combine the two commands into one, just join them with a semicolon:
sed -i 's/.../.../g;s/.../.../' test.m3u
Upvotes: 1