反转文本文件的行对的顺序

反转文本文件的行对的顺序

我想反转文本文件(m3u 播放列表)的行对的顺序。

这是m3u文件的原始内容:

#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

我需要像这样反转它:

#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

我已经这样了:

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

但这不是我需要的。

答案1

我会打印标题,交换每对中的行,然后反转整个事情:

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

tail -r如果tac您的系统上不可用,请尝试。另外,在某些设置上这不会起作用,因为它们head很愚蠢并且不符合标准- 另一种快速方法是通过sed q

{ sed q; { sed -n 'h;n;G;p' | tail -r; } ; } < infile

答案2

echo '#EXTM3U';grep -v ^#EXTM3U m3u |sed 'N;s/\n/ /'|sed -e 's/\(.*\)\(http.*\)$/\2\n\1/'|tac 

答案3

有一个更短的解决方案,没有tac,但只有headsed

$ 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

答案4

使用 Raku(以前称为 Perl_6)

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

输入示例:

#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

示例输出:

#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

Raku 的好处是它支持开箱即用的 Unicode,因此上面的 Raku 代码应该能够毫无困难地处理m3u和(Unicode 播放列表)。m3u8

暂时忽略第一#EXTM3U行(“”),Raku 代码非常简单。行被(惰性地)读入,rotor成对地一起读入,反向排序,并\n在每对之间打印换行符。

[唯一要记住的rotor是,如果元素数量(例如行)不足以完成rotor“集合”,它将在末尾默默地删除元素。如果您想在最后保留不完整的“集”,请使用batch而不是rotor]。

哦,是的,关于第一行:在 Raku 中,最简单的方法可能是插入对after but before#EXTM3U的调用,并在最后手动添加该行。.skip(1)lines.rotor#EXTM3U

https://raku.org

相关内容