1

I have an m3u file and I want to delete some lines from it. I know which channels I would like to keep from the m3u file. This list is of channels I want to keep is shorter than the list of channels I don't want.

Input file

#EXTM3U
#EXTINF:-1,ex-Yu: TV 1
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3168.ts
#EXTINF:-1,ex-Yu: TK Tuzla
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3164.ts
#EXTINF:-1,ex-Yu: SOS
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3191.ts
#EXTINF:-1,NL: NPO 1
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3190.ts
#EXTINF:-1,NL: NPO 2
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3167.ts
#EXTINF:-1,GB: Discovery
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3166.ts
#EXTINF:-1,GB: NGC
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3201.ts
#EXTINF:-1,NL: NPO 3
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3200.ts
#EXTINF:-1,IT: SKY Sport
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3198.ts
#EXTINF:-1,ex-Yu: Pink Film
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3199.ts
#EXTINF:-1,GB: Sky Sport
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3172.ts
#EXTINF:-1,ex-Yu: N1 Bosna
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3197.ts
#EXTINF:-1,DE: Bundesliga
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3195.ts
#EXTINF:-1,ex-Yu: MTV
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3170.ts
#EXTINF:-1,ex-Yu: Mini TV
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3177.ts
#EXTINF:-1,ex-Yu: M1 Film
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3179.ts
#EXTINF:-1,ex-Yu: Lov I Ribolov
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3196.ts
#EXTINF:-1,ex-Yu: Klasik TV
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3194.ts

Above is a shortened list of all channels. I know I would like to keep the following channels:

  • All channels starting with NL: (case sensitive)
  • All channels with sky sport in it (not case sensitive)
  • All channels with DE: Bundesliga in it

Wanted result

#EXTM3U
#EXTINF:-1,NL: NPO 1
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3190.ts
#EXTINF:-1,NL: NPO 2
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3167.ts
#EXTINF:-1,NL: NPO 3
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3200.ts
#EXTINF:-1,IT: SKY Sport
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3198.ts
#EXTINF:-1,GB: Sky Sport
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3172.ts
#EXTINF:-1,DE: Bundesliga
http://channel.domain.com:9603/live/YHJKUYII/02490j4/3195.ts

I can't get this to work in either sed, awk or any other bash tool which can do the job. Any ideas?

Mark Veenstra
  • 4,691
  • 6
  • 35
  • 66
  • 1
    Think positive! No, seriously it'll make your software MUCH easier to write and to understand later. When you write requirements that contain negative logic (`Delete multiple lines from an M3U file that does` **not** `match pattern`) you often end up with software that's written with negative logic and so hard to understand (and often wrong due to misunderstanding the [often-double] negative requirements) so just take a second to see if you could come up with a way to write the requirement in a positive way, e.g. `Select multiple lines from an M3U file that match pattern`. – Ed Morton Aug 13 '15 at 14:56

3 Answers3

3

Alternative awk command,

 awk '/NL:|DE: Bundesliga/ || tolower($0) ~ /sky sport/ {print; getline; print}'
karakfa
  • 66,216
  • 7
  • 41
  • 56
  • That will have negative side-effects if `getline` fails, you need to test for that, and it has all of the usual `getline` caveats. See http://awk.info/?tip/getline (and then don't use it anyway as this isn't a good application for getline). – Ed Morton Aug 13 '15 at 14:13
2

You can use the following command:

sed -n -r -e '/EXTM3U/p' -e '/NL:|DE: Bundesliga/,+1p' -e '/sky sport/I,+1p'

For each matching pattern, it prints the current line and the following line.

-n suppresses automatic printing of pattern, -r uses extended regular expressions, and -e allows to add multiple sed scripts on the same command.

/EXTM3U/p is the simplest: it matches lines containing EXTM3U and prints them (p)

For '/NL:|DE: Bundesliga/,+1p', it matches lines containing NL: or DE: Bundesliga, and prints it (p) as well as the following line (+1)

For /sky sport/I, I makes a case insensitive match.

cbliard
  • 7,051
  • 5
  • 41
  • 47
0
awk -F, 'f{print;f=0} ($2~/^NL:/) || /DE: Bundesliga/ || (tolower($0)~/sky sport/){print;f=1}' file
Ed Morton
  • 188,023
  • 17
  • 78
  • 185