2

I would like reverse the order of pairs of lines of a text file (m3u playlist).

This is the original content of the m3u file:

#EXTM3U
#EXTINF:-1,AL: Klan HD (AL)
http://mydomain.org:8000/live/username/mypass/1.ts
#EXTINF:-1,AL: Klan HD Beckup
http://mydomain.org:8000/live/username/mypass/2.ts
#EXTINF:-1,AL: Klan Kosova
http://mydomain.org:8000/live/username/mypass/3.ts
#EXTINF:-1,AL: Klan Kosova (B)
http://mydomain.org:8000/live/username/mypass/4.ts
#EXTINF:-1,AL: Top Channel SD
http://mydomain.org:8000/live/username/mypass/5.ts
#EXTINF:-1,AL: Top Channel HD
http://mydomain.org:8000/live/username/mypass/6.ts
#EXTINF:-1,AL: Top Channel Beckup
http://mydomain.org:8000/live/username/mypass/7.ts
#EXTINF:-1,AL: TV Uskana
http://mydomain.org:8000/live/username/mypass/8.ts

I need to reverse it like this:

#EXTM3U
#EXTINF:-1,AL: TV Uskana
http://mydomain.org:8000/live/username/mypass/8.ts
#EXTINF:-1,AL: Top Channel Beckup
http://mydomain.org:8000/live/username/mypass/7.ts
#EXTINF:-1,AL: Top Channel HD
http://mydomain.org:8000/live/username/mypass/6.ts
#EXTINF:-1,AL: Top Channel SD
http://mydomain.org:8000/live/username/mypass/5.ts
#EXTINF:-1,AL: Klan Kosova (B)
http://mydomain.org:8000/live/username/mypass/4.ts
#EXTINF:-1,AL: Klan Kosova
http://mydomain.org:8000/live/username/mypass/3.ts
#EXTINF:-1,AL: Klan HD Beckup
http://mydomain.org:8000/live/username/mypass/2.ts
#EXTINF:-1,AL: Klan HD (AL)
http://mydomain.org:8000/live/username/mypass/1.ts

I have already got it like this:

http://mydomain.org:8000/live/username/mypass/8.ts
#EXTINF:-1,AL: TV Uskana
http://mydomain.org:8000/live/username/mypass/7.ts
#EXTINF:-1,AL: Top Channel Beckup
http://mydomain.org:8000/live/username/mypass/6.ts
#EXTINF:-1,AL: Top Channel HD
http://mydomain.org:8000/live/username/mypass/5.ts
#EXTINF:-1,AL: Top Channel SD
http://mydomain.org:8000/live/username/mypass/4.ts
#EXTINF:-1,AL: Klan Kosova (B)
http://mydomain.org:8000/live/username/mypass/3.ts
#EXTINF:-1,AL: Klan Kosova
http://mydomain.org:8000/live/username/mypass/2.ts
#EXTINF:-1,AL: Klan HD Beckup
http://mydomain.org:8000/live/username/mypass/1.ts
#EXTINF:-1,AL: Klan HD (AL)
#EXTM3U

But it isn't what I need.

0

4 Answers 4

2

I'd print the header, swap lines in each pair then reverse the whole thing:

{ head -n 1; { sed -n 'h;n;G;p' | tac; } ; } < infile

Try tail -r if tac is not available on your system. Also, on some setups this won't work because their head is dumb and won't comply with the standards - the other quick way to do it is via sed q:

{ sed q; { sed -n 'h;n;G;p' | tail -r; } ; } < infile
0
echo '#EXTM3U';grep -v ^#EXTM3U m3u |sed 'N;s/\n/ /'|sed -e 's/\(.*\)\(http.*\)$/\2\n\1/'|tac 
4
  • 1
    Maybe you could use the hold space to move line 1? sed -e '1{h;d}' -e '$!N; s/\(.*\)\n\(.*\)/\2\n\1/' -e '$G' file.m3u | tac Jan 11, 2017 at 3:36
  • Thanks so, how can save the change in the same file again ?
    – Adem Aga
    Jan 11, 2017 at 13:33
  • use temporary file Jan 11, 2017 at 13:46
  • save the change in the same file, using sponge, from moreutils
    – evandrix
    Oct 16, 2017 at 16:10
0

There's a shorter solution, without tac, but with only head and sed :

$ head -n 1 f;sed -n '1!{N;2,3!G;h;$p}' f;
#EXTM3U
#EXTINF:-1,AL: TV Uskana
http://mydomain.org:8000/live/username/mypass/8.ts
#EXTINF:-1,AL: Top Channel Beckup
http://mydomain.org:8000/live/username/mypass/7.ts
#EXTINF:-1,AL: Top Channel HD
http://mydomain.org:8000/live/username/mypass/6.ts
#EXTINF:-1,AL: Top Channel SD
http://mydomain.org:8000/live/username/mypass/5.ts
#EXTINF:-1,AL: Klan Kosova (B)
http://mydomain.org:8000/live/username/mypass/4.ts
#EXTINF:-1,AL: Klan Kosova
http://mydomain.org:8000/live/username/mypass/3.ts
#EXTINF:-1,AL: Klan HD Beckup
http://mydomain.org:8000/live/username/mypass/2.ts
#EXTINF:-1,AL: Klan HD (AL)
http://mydomain.org:8000/live/username/mypass/1.ts
0

Using Raku (formerly known as Perl_6)

raku -e '.join("\n").put for lines.rotor(2).reverse;'

Sample Input:

#EXTINF:-1,AL: Klan HD (AL)
http://mydomain.org:8000/live/username/mypass/1.ts
#EXTINF:-1,AL: Klan HD Beckup
http://mydomain.org:8000/live/username/mypass/2.ts
#EXTINF:-1,AL: Klan Kosova
http://mydomain.org:8000/live/username/mypass/3.ts
#EXTINF:-1,AL: Klan Kosova (B)
http://mydomain.org:8000/live/username/mypass/4.ts
#EXTINF:-1,AL: Top Channel SD
http://mydomain.org:8000/live/username/mypass/5.ts
#EXTINF:-1,AL: Top Channel HD
http://mydomain.org:8000/live/username/mypass/6.ts
#EXTINF:-1,AL: Top Channel Beckup
http://mydomain.org:8000/live/username/mypass/7.ts
#EXTINF:-1,AL: TV Uskana
http://mydomain.org:8000/live/username/mypass/8.ts

Sample Output:

#EXTINF:-1,AL: TV Uskana
http://mydomain.org:8000/live/username/mypass/8.ts
#EXTINF:-1,AL: Top Channel Beckup
http://mydomain.org:8000/live/username/mypass/7.ts
#EXTINF:-1,AL: Top Channel HD
http://mydomain.org:8000/live/username/mypass/6.ts
#EXTINF:-1,AL: Top Channel SD
http://mydomain.org:8000/live/username/mypass/5.ts
#EXTINF:-1,AL: Klan Kosova (B)
http://mydomain.org:8000/live/username/mypass/4.ts
#EXTINF:-1,AL: Klan Kosova
http://mydomain.org:8000/live/username/mypass/3.ts
#EXTINF:-1,AL: Klan HD Beckup
http://mydomain.org:8000/live/username/mypass/2.ts
#EXTINF:-1,AL: Klan HD (AL)
http://mydomain.org:8000/live/username/mypass/1.ts

The nice thing about Raku is it supports Unicode out-of-the-box, so the Raku code above should be able to handle both m3u and m3u8 (Unicode Playlists) with no difficulty.

Ignoring the first ("#EXTM3U") line for the moment, the Raku code is pretty straight-forward. Lines are read in (lazily), rotor-ed together in pairs, reverse-ordered, and printed with a \n newline in-between each pair.

[The only thing to remember with rotor is that it will silently drop elements at the end if there is an insufficient # of elements (e.g. lines) to complete a rotor "set". If you want to preserve incomplete "sets" at the end, use batch instead of rotor].

Oh yes, about that initial #EXTM3U line: in Raku it's probably easiest to interpose a call to .skip(1) after lines but before .rotor, and add that #EXTM3U line back manually at the end.

https://raku.org

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .