S
S
SOTVM2020-01-12 12:03:06
linux
SOTVM, 2020-01-12 12:03:06

How to change the order of lines in a file?

I do not want to reinvent the wheel,
maybe someone has already solved a similar problem.

we have a youtube.m3u8 playlist

#EXTM3U
#EXTINF:0,ПАВЛИК 4 сезон 10 серия
https://www.youtube.com/watch?v=z4oMY9Q0HOg
#EXTINF:0,ПАВЛИК 4 сезон 9 серия
https://www.youtube.com/watch?v=Vk_nZ8_V5ko
#EXTINF:0,ПАВЛИК 4 сезон 8 серия
https://www.youtube.com/watch?v=LB5VAm2b4iA
#EXTINF:0,ПАВЛИК 4 сезон 7 серия
https://www.youtube.com/watch?v=kjJMTnU9GCg
#EXTINF:0,ПАВЛИК 4 сезон 6 серия
https://www.youtube.com/watch?v=hGsITAB76_4
#EXTINF:0,ПАВЛИК 4 сезон 5 серия
https://www.youtube.com/watch?v=MCJUj3EgiYk
#EXTINF:0,ПАВЛИК 4 сезон 4 серия
https://www.youtube.com/watch?v=M-VGl5lxyC8
#EXTINF:0,ПАВЛИК 4 сезон 3 серия
https://www.youtube.com/watch?v=cNwUqDbXLiY
#EXTINF:0,ПАВЛИК 4 сезон 2 серия
https://www.youtube.com/watch?v=jHYW6mq7D24
#EXTINF:0,ПАВЛИК 4 сезон 1 серия
https://www.youtube.com/watch?v=RBDiRbE4yoI
#EXTINF:0,ПАВЛИК 4 сезон - Трейлер
https://www.youtube.com/watch?v=pjJfQ7Nw568


you need to change the video order in it, 10-5 4 3 2 1 by 1 2 3 4 5-10 I
present such an algorithm
1) copy the 1st line of the source file list.m3u8 to new.list.m3u8
delete the 1st line from the source .
2) copy the last 2 lines of the list.m3u8 file and >> add to new.list.m3u8
delete the last 2 lines of the list.m3u8 file
3) repeat point 2) while there is something to copy in the source file

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vreitech, 2020-01-12
@sotvm

perl -i -0ne 's/#EXTM3U\n//g;s/(#EXTINF:.*)\n(http.*)/\2\n\1/g;$_.="#EXTM3U\n";print join("\n",reverse(split("\n")))' ./youtube.m3u8

S
Sergey Karbivnichy, 2020-01-12
@hottabxp

If I understand correctly, then the tac command (reverse cat) will help you.
tac list.m3u8 > list2.m3u8
https://www.youtube.com/watch?v=pjJfQ7Nw568
#EXTINF:0,ПАВЛИК 4 сезон - Трейлер
https://www.youtube.com/watch?v=RBDiRbE4yoI
#EXTINF:0,ПАВЛИК 4 сезон 1 серия
https://www.youtube.com/watch?v=jHYW6mq7D24
#EXTINF:0,ПАВЛИК 4 сезон 2 серия
https://www.youtube.com/watch?v=cNwUqDbXLiY
#EXTINF:0,ПАВЛИК 4 сезон 3 серия
https://www.youtube.com/watch?v=M-VGl5lxyC8
#EXTINF:0,ПАВЛИК 4 сезон 4 серия
https://www.youtube.com/watch?v=MCJUj3EgiYk
#EXTINF:0,ПАВЛИК 4 сезон 5 серия
https://www.youtube.com/watch?v=hGsITAB76_4
#EXTINF:0,ПАВЛИК 4 сезон 6 серия
https://www.youtube.com/watch?v=kjJMTnU9GCg
#EXTINF:0,ПАВЛИК 4 сезон 7 серия
https://www.youtube.com/watch?v=LB5VAm2b4iA
#EXTINF:0,ПАВЛИК 4 сезон 8 серия
https://www.youtube.com/watch?v=Vk_nZ8_V5ko
#EXTINF:0,ПАВЛИК 4 сезон 9 серия
https://www.youtube.com/watch?v=z4oMY9Q0HOg
#EXTINF:0,ПАВЛИК 4 сезон 10 серия
#EXTM3U


Well, you need to modify a little!

V
Vadim Priluzkiy, 2020-01-12
@Oxyd

head -n 1 list.m3u8 > new.list.m3u8 ; tac list.m3u8 | head -n -1 >> new.list.m3u8

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question