使用 sed 一次对 2 行进行排序

Sorting 2 lines at a time with sed

提问人:john Wallace 提问时间:7/30/2018 最后编辑:john Wallace 更新时间:8/3/2018 访问量:191

问:

我想用它来对大型播放列表文件进行排序。每个播放列表项都有 2 行,因此我需要根据第一行之后的字符串进行排序(因此在本例中,第一行将是 UK:Button 1(SG)),对第一行和下一行进行排序(以 开头的行)。sedtvg-name=http

我见过非常相似的正则表达式示例,但语法超出了我的范围,无法进行逆向工程以适应我的文件。谁能帮忙?

#EXTM3U
#EXTINF:-1 tvg-id="BBCRedButton.uk" tvg-name="UK: BBC Red Button 1 (SG)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC Red Button 1 (SG)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/112233.ts
#EXTINF:-1 tvg-id="BBC1London.uk" tvg-name="UK: BBC ONE (HD) (720p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC ONE (HD) (720p)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/36011.ts
#EXTINF:-1 tvg-id="BBC1Scotland.uk" tvg-name="UK: BBC One Scot FHD (1080p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC One Scot FHD (1080p)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/24651.ts
#EXTINF:-1 tvg-id="" tvg-name="Act Of Grace" tvg-logo="http://wesite.test:8080/images/skQxxCIxEWuXL4tmPcfDoFjtSZU_small.jpg" group-title="_Movies_",Act Of Grace
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/102408.mp4
#EXTINF:-1 tvg-id="" tvg-name="Act Of Valor" tvg-logo="http://wesite.test:8080/images/1Xcd5ci69pVXMPP02DU11Ffq0yY_small.jpg" group-title="_Movies_",Act Of Valor
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/96873.mp4
#EXTINF:-1 tvg-id="" tvg-name="Action Jackson" tvg-logo="http://wesite.test:8080/images/rg5WY1SiyPDuTYZs2vgTV0csVbz_small.jpg" group-title="_Movies_",Action Jackson
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/96874.mp4
#EXTINF:-1 tvg-id="" tvg-name="Acts Of Vengeance" tvg-logo="http://wesite.test:8080/images/r5o6vWPYOQs6bv91gQ8kQs2zQYl_small.jpg" group-title="_Movies_",Acts Of Vengeance
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/107101.mp4
#EXTINF:-1 tvg-id="" tvg-name="Creed" tvg-logo="http://wesite.test:8080/images/hKzhV274pkZBSpXfCjUyzbyYKLl_small.jpg" group-title="Drama",Creed
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/102773.mp4
#EXTINF:-1 tvg-id="" tvg-name="Creep Van" tvg-logo="http://wesite.test:8080/images/r2tSTcns0gHVynnLVPwCtTePfOt_small.jpg" group-title="Horror",Creep Van
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/105306.mp4
#EXTINF:-1 tvg-id="" tvg-name="Creepozoids" tvg-logo="http://wesite.test:8080/images/gZ3HBNBYe6hDTsocsXjDYGv0ZXD_small.jpg" group-title="",Creepozoids
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/106831.mp4
#EXTINF:-1 tvg-id="" tvg-name="Creepshow 2" tvg-logo="http://wesite.test:8080/images/qxJWtBb89RaSRgLuz1d6ZuFTfVG_small.jpg" group-title="Horror",Creepshow 2
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/105307.mp4
#EXTINF:-1 tvg-id="" tvg-name="Acts Of Violence" tvg-logo="http://wesite.test:8080/images/pK9CugRd3DIP0THBH8WlGrvk5vy_small.jpg" group-title="_Movies_",Acts Of Violence
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/96711.mp4
#EXTINF:-1 tvg-id="" tvg-name="Adaptation" tvg-logo="http://wesite.test:8080/images/5trb1V5f3IsjpZx2GiuUylowl3W_small.jpg" group-title="_Movies_",Adaptation
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/99509.mp4

预期输出 :

#EXTM3U
#EXTINF:-1 tvg-id="BBCRedButton.uk" tvg-name="UK: BBC Red Button 1 (SG)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC Red Button 1 (SG)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/112233.ts
#EXTINF:-1 tvg-id="BBC1London.uk" tvg-name="UK: BBC ONE (HD) (720p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC ONE (HD) (720p)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/36011.ts
#EXTINF:-1 tvg-id="BBC1Scotland.uk" tvg-name="UK: BBC One Scot FHD (1080p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC One Scot FHD (1080p)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/24651.ts
#EXTINF:-1 tvg-id="" tvg-name="Act Of Grace" tvg-logo="http://wesite.test:8080/images/skQxxCIxEWuXL4tmPcfDoFjtSZU_small.jpg" group-title="_Movies_",Act Of Grace
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/102408.mp4
#EXTINF:-1 tvg-id="" tvg-name="Act Of Valor" tvg-logo="http://wesite.test:8080/images/1Xcd5ci69pVXMPP02DU11Ffq0yY_small.jpg" group-title="_Movies_",Act Of Valor
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/96873.mp4
#EXTINF:-1 tvg-id="" tvg-name="Action Jackson" tvg-logo="http://wesite.test:8080/images/rg5WY1SiyPDuTYZs2vgTV0csVbz_small.jpg" group-title="_Movies_",Action Jackson
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/96874.mp4
#EXTINF:-1 tvg-id="" tvg-name="Acts Of Vengeance" tvg-logo="http://wesite.test:8080/images/r5o6vWPYOQs6bv91gQ8kQs2zQYl_small.jpg" group-title="_Movies_",Acts Of Vengeance
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/107101.mp4
#EXTINF:-1 tvg-id="" tvg-name="Acts Of Violence" tvg-logo="http://wesite.test:8080/images/pK9CugRd3DIP0THBH8WlGrvk5vy_small.jpg" group-title="_Movies_",Acts Of Violence
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/96711.mp4
#EXTINF:-1 tvg-id="" tvg-name="Adaptation" tvg-logo="http://wesite.test:8080/images/5trb1V5f3IsjpZx2GiuUylowl3W_small.jpg" group-title="_Movies_",Adaptation
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/99509.mp4   
#EXTINF:-1 tvg-id="" tvg-name="Creed" tvg-logo="http://wesite.test:8080/images/hKzhV274pkZBSpXfCjUyzbyYKLl_small.jpg" group-title="Drama",Creed
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/102773.mp4
#EXTINF:-1 tvg-id="" tvg-name="Creep Van" tvg-logo="http://wesite.test:8080/images/r2tSTcns0gHVynnLVPwCtTePfOt_small.jpg" group-title="Horror",Creep Van
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/105306.mp4
#EXTINF:-1 tvg-id="" tvg-name="Creepozoids" tvg-logo="http://wesite.test:8080/images/gZ3HBNBYe6hDTsocsXjDYGv0ZXD_small.jpg" group-title="",Creepozoids
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/106831.mp4
#EXTINF:-1 tvg-id="" tvg-name="Creepshow 2" tvg-logo="http://wesite.test:8080/images/qxJWtBb89RaSRgLuz1d6ZuFTfVG_small.jpg" group-title="Horror",Creepshow 2
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/105307.mp4

我从我的母版那里复制并粘贴了一部分电影,但两个脚本都没有对输出进行更改。示例如下:

输入:

#EXTINF:-1 tvg-id="" tvg-name="The Santa Clause" tvg-logo="http://liquidit.info:8080/images/hrZjAYAF1o37k4Qb442c4yxwVLw_small.jpg" group-title=_Movies_",The Santa Clause
http://liquidit.info:8080/movie/Wallace47B/2IhPHmO9Q8/104987.mp4
#EXTINF:-1 tvg-id="" tvg-name="The Santa Clause 2" tvg-logo="http://liquidit.info:8080/images/i7tbiDPIaa4VsQh1wWmbkY4zTRX_small.jpg" group-title=_Movies_",The Santa Clause 2
http://liquidit.info:8080/movie/Wallace47B/2IhPHmO9Q8/104988.mp4
#EXTINF:-1 tvg-id="" tvg-name="The Santa Clause 3 The Escape Clause" tvg-logo="http://liquidit.info:8080/images/kvKXyrc3cUGqXin2u76Ef8lApMI_small.jpg" group-title=_Movies_",The Santa Clause 3 The Escape Clause
http://liquidit.info:8080/movie/Wallace47B/2IhPHmO9Q8/101023.mp4
#EXTINF:-1 tvg-id="" tvg-name="The Sapphires" tvg-logo="http://liquidit.info:8080/images/h7zn7Sf0Jl6mFZjGj4TCHjSJj6T_small.jpg" group-title=_Movies_",The Sapphires
http://liquidit.info:8080/movie/Wallace47B/2IhPHmO9Q8/101024.mp4
#EXTINF:-1 tvg-id="" tvg-name="Fracture" tvg-logo="http://liquidit.info:8080/images/sl5QYze20MclzDLxLDqe3sEJdiW_small.jpg" group-title=_Movies_",Fracture
http://liquidit.info:8080/movie/Wallace47B/2IhPHmO9Q8/107279.mp4

预期的 Ouptut 将是顶部的电影项目“骨折”,因为 alpabeticalized 排序。

#EXTINF:-1 tvg-id="" tvg-name="Fracture" tvg-logo="http://liquidit.info:8080/images/sl5QYze20MclzDLxLDqe3sEJdiW_small.jpg" group-title=_Movies_",Fracture
http://liquidit.info:8080/movie/Wallace47B/2IhPHmO9Q8/107279.mp4  
#EXTINF:-1 tvg-id="" tvg-name="The Santa Clause" tvg-logo="http://liquidit.info:8080/images/hrZjAYAF1o37k4Qb442c4yxwVLw_small.jpg" group-title=_Movies_",The Santa Clause
http://liquidit.info:8080/movie/Wallace47B/2IhPHmO9Q8/104987.mp4
#EXTINF:-1 tvg-id="" tvg-name="The Santa Clause 2" tvg-logo="http://liquidit.info:8080/images/i7tbiDPIaa4VsQh1wWmbkY4zTRX_small.jpg" group-title=_Movies_",The Santa Clause 2
http://liquidit.info:8080/movie/Wallace47B/2IhPHmO9Q8/104988.mp4
#EXTINF:-1 tvg-id="" tvg-name="The Santa Clause 3 The Escape Clause" tvg-logo="http://liquidit.info:8080/images/kvKXyrc3cUGqXin2u76Ef8lApMI_small.jpg" group-title=_Movies_",The Santa Clause 3 The Escape Clause
http://liquidit.info:8080/movie/Wallace47B/2IhPHmO9Q8/101023.mp4
#EXTINF:-1 tvg-id="" tvg-name="The Sapphires" tvg-logo="http://liquidit.info:8080/images/h7zn7Sf0Jl6mFZjGj4TCHjSJj6T_small.jpg" group-title=_Movies_",The Sapphires
http://liquidit.info:8080/movie/Wallace47B/2IhPHmO9Q8/101024.mp4
正则表达式 排序 SED

评论

0赞 Ed Morton 8/2/2018
解释为什么这是给定您发布的示例输入的预期输出。“Acts of Violence”和“Adaptation”只是从输入文件的末尾跳到输出文件的中间 - 这不仅仅是对记录进行排序,所以解释一个程序应该如何知道才能做到这一点。如果所有记录最终都应该组合在一起,那么程序应该如何决定将该分组放在输出中的哪个位置?第一部电影在输入中的位置,或者大多数电影的位置或其他什么?_Movies__Movies_
0赞 john Wallace 8/2/2018
我没有意识到 Creepozoids 和 Creepshow 没有组名 Movies(对不起)。我正在尝试做的是根据 tvg-name 后面的字符串按字母顺序对组名电影中的行进行排序。
0赞 Ed Morton 8/2/2018
所以我在答案中包含的最后 2 个脚本中的一个完全符合您的要求,对吧?
0赞 john Wallace 8/3/2018
嗨 ED - 这两个选项似乎都没有修改脚本 - 我在帖子中添加了第二个示例输入和输出。
0赞 Ed Morton 8/3/2018
对不起,我不能继续剥洋葱了。正如您在我的回答中看到的,我提供的脚本确实将您提供的输入转换为您提供的输出。

答:

1赞 Beta 7/30/2018 #1

如果你想用像 sed 这样的工具来做到这一点,你必须 A) 相信你的字段不包含任何恶性的东西,或者 B) 编写极其艰苦的脚本。tvg-id="http://...

我会尝试一些粗糙但有效的东西,比如这样。首先,将两行合并为一行:

sed 'N;s/\n//'

然后将该字段复制到该行的前面:tvg-name

sed 's/\(.*tvg-name=\)\("[^"]*"\)/\2\1\2/'

然后排序:

sort

然后删除我添加的字段:

sed 's/^"[^"]*"//'

然后将一行分成两行:

sed 'h;s/http.*//;p;g;s/.*http/http/'

把它们放在一起:

sed 'N;s/\n//;s/\(.*tvg-name=\)\("[^"]*"\)/\2\1\2/' filename | sort | sed 's/^"[^"]*"//;h;s/http.*//;p;g;s/.*http/http/'
0赞 potong 7/31/2018 #2

这可能对你有用(GNU sed & sort):

sed 'N;s/^\(.*\)\n/\1\1/;2h;2!H;$!d;x;s/.*/echo '\''&'\'' | sort -k3,3/e;s/^\(.*\)\1/\1\n/Mg' file

通过复制第一行并删除第一行和第二行之间的换行符,将每两行记录转换为单行。将整个文件收集到保留空间中保存的内存中。在文件末尾,交换到保留空间,并使用 GNU sed 的 evaluate 命令作为替换命令中的标志,对单行记录中第三个字段的文件进行排序。将单行记录重新格式化为两行记录并打印结果。

评论

0赞 john Wallace 7/31/2018
嗨,这两个答案似乎都行不通。排序似乎不起作用,按字母顺序排列行。此外,当我再次拆分行时,文件只有一半大小。
0赞 potong 7/31/2018
@johnWallace我错误地留下了一个额外的命令,忘记正确引用 echo 命令。如果我使用你的(短)数据,在我看来,可以按照你的意愿做,但我只是猜测。也许您可以在之前/之后提供更多数据。
0赞 john Wallace 8/1/2018
嗨,Potongget,我在执行代码时收到“sed:替换表达式中的错误选项”。
0赞 Ed Morton 7/31/2018 #3

如果没有更好的样本输入和预期的输出,这只是一个猜测,但这可能是您正在尝试执行的操作:

$ awk -F'"' -v OFS='\t' 'NF>1{key=$4; cnt=0} {print key, ++cnt, $0}' file | sort -t$'\t' -k1,1 -k2,2n -s | cut -f3-
#EXTINF:-1 tvg-id="Button.uk" tvg-name="UK: Button 1 (SG)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: Button 1 (SG)
#http://website.info:8080/live/xxxxxxxx/xxxxxxxx/112233.ts
##EXTINF:-1 tvg-id="London.uk" tvg-name="UK: London (HD) (720p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: London (HD) (720p)
#http://website.info:8080/live/xxxxxxxx/xxxxxxxx/36011.ts

或者,如果您希望它以相反的方式排序:

$ awk -F'"' -v OFS='\t' 'NF>1{key=$4; cnt=0} {print key, ++cnt, $0}' file | sort -t$'\t' -k1,1r -k2,2n -s | cut -f3-
##EXTINF:-1 tvg-id="London.uk" tvg-name="UK: London (HD) (720p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: London (HD) (720p)
#http://website.info:8080/live/xxxxxxxx/xxxxxxxx/36011.ts
#EXTINF:-1 tvg-id="Button.uk" tvg-name="UK: Button 1 (SG)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: Button 1 (SG)
#http://website.info:8080/live/xxxxxxxx/xxxxxxxx/112233.ts

它只是在每一行前面加上引号之间的键值,然后是从该值开始的行计数,然后按添加的字段对其进行排序,然后再次删除添加的字段。如果你看一下它是如何分阶段工作的,就很容易理解了:

1) 添加用于排序的 2 个前缀字段:

$ awk -F'"' -v OFS='\t' 'NF>1{key=$4; cnt=0} {print key, ++cnt, $0}' file
UK: Button 1 (SG)       1       #EXTINF:-1 tvg-id="Button.uk" tvg-name="UK: Button 1 (SG)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: Button 1 (SG)
UK: Button 1 (SG)       2       #http://website.info:8080/live/xxxxxxxx/xxxxxxxx/112233.ts
UK: London (HD) (720p)  1       ##EXTINF:-1 tvg-id="London.uk" tvg-name="UK: London (HD) (720p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: London (HD) (720p)
UK: London (HD) (720p)  2       #http://website.info:8080/live/xxxxxxxx/xxxxxxxx/36011.ts

2) 使用步骤 1 中添加的 2 个字段对输出进行排序:

$ awk -F'"' -v OFS='\t' 'NF>1{key=$4; cnt=0} {print key, ++cnt, $0}' file |
    sort -t$'\t' -k1,1r -k2,2n -s
UK: London (HD) (720p)  1       ##EXTINF:-1 tvg-id="London.uk" tvg-name="UK: London (HD) (720p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: London (HD) (720p)
UK: London (HD) (720p)  2       #http://website.info:8080/live/xxxxxxxx/xxxxxxxx/36011.ts
UK: Button 1 (SG)       1       #EXTINF:-1 tvg-id="Button.uk" tvg-name="UK: Button 1 (SG)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: Button 1 (SG)
UK: Button 1 (SG)       2       #http://website.info:8080/live/xxxxxxxx/xxxxxxxx/112233.ts

3) 删除在步骤 2 中添加的 1 个字段:

$ awk -F'"' -v OFS='\t' 'NF>1{key=$4; cnt=0} {print key, ++cnt, $0}' file |
    sort -t$'\t' -k1,1r -k2,2n -s |
    cut -f3-
##EXTINF:-1 tvg-id="London.uk" tvg-name="UK: London (HD) (720p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: London (HD) (720p)
#http://website.info:8080/live/xxxxxxxx/xxxxxxxx/36011.ts
#EXTINF:-1 tvg-id="Button.uk" tvg-name="UK: Button 1 (SG)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: Button 1 (SG)
#http://website.info:8080/live/xxxxxxxx/xxxxxxxx/112233.ts

请注意,上述方法适用于多行块中任意数量的行,并且相同的方法适用于对多个不同的子字符串进行排序。它也非常高效(在内存中几乎不存储任何内容),并且易于以后调试/增强/维护。

编辑1:根据您在下面的评论和您提供的以下新示例输入,我是您想要的,但我不知道它是否是,因为没有提供预期的输出,而且我能最好地告诉您提供的示例输入已经排序,所以无法判断该工具是否正在做任何事情。

$ cat tst.awk
BEGIN {
    FS  = "\""
    OFS = "\t"
}
/^#/ {
    tvgName        = $4
    nameRowNr      = 0
    prevGroupTitle = groupTitle
    groupTitle     = $8
    if ( ! ((groupTitle == "_Movies_") && (groupTitle == prevGroupTitle)) ) {
        groupNr++
    }
}
{
    print groupNr, tvgName, ++nameRowNr, $0
}

.

$ awk -f tst.awk file | sort -t$'\t' -k1,1n -k2,2 -k3,3n -s | cut -f4-
#EXTM3U
#EXTINF:-1 tvg-id="BBCRedButton.uk" tvg-name="UK: BBC Red Button 1 (SG)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC Red Button 1 (SG)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/112233.ts
#EXTINF:-1 tvg-id="BBC1London.uk" tvg-name="UK: BBC ONE (HD) (720p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC ONE (HD) (720p)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/36011.ts
#EXTM3U
#EXTINF:-1 tvg-id="BBCRedButton.uk" tvg-name="UK: BBC Red Button 1 (SG)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC Red Button 1 (SG)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/112233.ts
#EXTINF:-1 tvg-id="BBC1London.uk" tvg-name="UK: BBC ONE (HD) (720p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC ONE (HD) (720p)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/36011.ts
#EXTINF:-1 tvg-id="BBC1Scotland.uk" tvg-name="UK: BBC One Scot FHD (1080p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC One Scot FHD (1080p)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/24651.ts
#EXTINF:-1 tvg-id="" tvg-name="Act Of Grace" tvg-logo="http://wesite.test:8080/images/skQxxCIxEWuXL4tmPcfDoFjtSZU_small.jpg" group-title="_Movies_",Act Of Grace
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/102408.mp4
#EXTINF:-1 tvg-id="" tvg-name="Act Of Valor" tvg-logo="http://wesite.test:8080/images/1Xcd5ci69pVXMPP02DU11Ffq0yY_small.jpg" group-title="_Movies_",Act Of Valor
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/96873.mp4
#EXTINF:-1 tvg-id="" tvg-name="Action Jackson" tvg-logo="http://wesite.test:8080/images/rg5WY1SiyPDuTYZs2vgTV0csVbz_small.jpg" group-title="_Movies_",Action Jackson
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/96874.mp4
#EXTINF:-1 tvg-id="" tvg-name="Acts Of Vengeance" tvg-logo="http://wesite.test:8080/images/r5o6vWPYOQs6bv91gQ8kQs2zQYl_small.jpg" group-title="_Movies_",Acts Of Vengeance
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/107101.mp4
#EXTINF:-1 tvg-id="" tvg-name="Acts Of Violence" tvg-logo="http://wesite.test:8080/images/pK9CugRd3DIP0THBH8WlGrvk5vy_small.jpg" group-title="_Movies_",Acts Of Violence
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/96711.mp4
#EXTINF:-1 tvg-id="" tvg-name="Adaptation" tvg-logo="http://wesite.test:8080/images/5trb1V5f3IsjpZx2GiuUylowl3W_small.jpg" group-title="_Movies_",Adaptation
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/99509.mp4

编辑 2:鉴于现在在您的问题中发布的更新的示例输入和预期输出,这可能是您想要的:

$ cat tst.awk
BEGIN {
    FS  = "\""
    OFS = "\t"
}
/^#/ {
    tvgName        = $4
    nameRowNr      = 0
    groupTitle     = $8
    if ( groupTitle == "_Movies_" ) {
        if ( !(groupTitle in groupTitle2nr) ) {
            groupTitle2nr[groupTitle] = ++numGroups
        }
        groupNr = groupTitle2nr[groupTitle]
    }
    else {
        groupNr = ++numGroups
    }
}
{
    print groupNr, tvgName, ++nameRowNr, $0
}

.

$ awk -f tst.awk file | sort -t$'\t' -k1,1n -k2,2 -k3,3n -s | cut -f4-
#EXTM3U
#EXTINF:-1 tvg-id="BBCRedButton.uk" tvg-name="UK: BBC Red Button 1 (SG)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC Red Button 1 (SG)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/112233.ts
#EXTINF:-1 tvg-id="BBC1London.uk" tvg-name="UK: BBC ONE (HD) (720p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC ONE (HD) (720p)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/36011.ts
#EXTINF:-1 tvg-id="BBC1Scotland.uk" tvg-name="UK: BBC One Scot FHD (1080p)" tvg-logo="" group-title="ENTERTAINMENT & LIFESTYLE UK",UK: BBC One Scot FHD (1080p)
http://wesite.test:8080/live/XXXXXXXX/XXXXXXXXX/24651.ts
#EXTINF:-1 tvg-id="" tvg-name="Act Of Grace" tvg-logo="http://wesite.test:8080/images/skQxxCIxEWuXL4tmPcfDoFjtSZU_small.jpg" group-title="_Movies_",Act Of Grace
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/102408.mp4
#EXTINF:-1 tvg-id="" tvg-name="Act Of Valor" tvg-logo="http://wesite.test:8080/images/1Xcd5ci69pVXMPP02DU11Ffq0yY_small.jpg" group-title="_Movies_",Act Of Valor
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/96873.mp4
#EXTINF:-1 tvg-id="" tvg-name="Action Jackson" tvg-logo="http://wesite.test:8080/images/rg5WY1SiyPDuTYZs2vgTV0csVbz_small.jpg" group-title="_Movies_",Action Jackson
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/96874.mp4
#EXTINF:-1 tvg-id="" tvg-name="Acts Of Vengeance" tvg-logo="http://wesite.test:8080/images/r5o6vWPYOQs6bv91gQ8kQs2zQYl_small.jpg" group-title="_Movies_",Acts Of Vengeance
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/107101.mp4
#EXTINF:-1 tvg-id="" tvg-name="Acts Of Violence" tvg-logo="http://wesite.test:8080/images/pK9CugRd3DIP0THBH8WlGrvk5vy_small.jpg" group-title="_Movies_",Acts Of Violence
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/96711.mp4
#EXTINF:-1 tvg-id="" tvg-name="Adaptation" tvg-logo="http://wesite.test:8080/images/5trb1V5f3IsjpZx2GiuUylowl3W_small.jpg" group-title="_Movies_",Adaptation
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/99509.mp4
#EXTINF:-1 tvg-id="" tvg-name="Creed" tvg-logo="http://wesite.test:8080/images/hKzhV274pkZBSpXfCjUyzbyYKLl_small.jpg" group-title="Drama",Creed
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/102773.mp4
#EXTINF:-1 tvg-id="" tvg-name="Creep Van" tvg-logo="http://wesite.test:8080/images/r2tSTcns0gHVynnLVPwCtTePfOt_small.jpg" group-title="Horror",Creep Van
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/105306.mp4
#EXTINF:-1 tvg-id="" tvg-name="Creepozoids" tvg-logo="http://wesite.test:8080/images/gZ3HBNBYe6hDTsocsXjDYGv0ZXD_small.jpg" group-title="",Creepozoids
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/106831.mp4
#EXTINF:-1 tvg-id="" tvg-name="Creepshow 2" tvg-logo="http://wesite.test:8080/images/qxJWtBb89RaSRgLuz1d6ZuFTfVG_small.jpg" group-title="Horror",Creepshow 2
http://wesite.test:8080/movie/XXXXXXXX/XXXXXXXXX/105307.mp4

上面的脚本比它必须的稍微复杂一些,所以如果/当你将来需要通过调整行来这样做时,添加额外的“组标题”以在工具的同一调用中对所有内容进行排序是微不足道的。if ( groupTitle == "_Movies_" )

评论

0赞 john Wallace 8/1/2018
嗨,埃德, - 您的脚本几乎可以与我的完整文件一起使用。很抱歉很痛苦,我对脚本很陌生,所以试图在我去的时候拿起这个。是否可以发布完整的文件供您检查(如果您有时间?我想对“电影”组标题中的所有标题进行 AZ 排序(使事情变得更加复杂)
0赞 john Wallace 8/1/2018
Ed - 对不起,加入文件是指另一个用户建议的组合行。我不认为它与你的剧本有关?
0赞 Ed Morton 8/1/2018
约翰 - 不,你必须在你的问题中创建一个最小的可重复的例子,以及你希望我们考虑的任何输入变化。如果不清楚,请参阅如何询问。首先 - 听起来您在完整文件中发现了 1 个或多个条目,而我的脚本不起作用,可能是因为它遵循的格式与现有条目不同,因此将其添加到输入中的示例中,以及任何其他失败且格式明显不同的条目。不,听起来“加入的文件”与我的脚本没有任何关系。
0赞 john Wallace 8/1/2018
嗨,Ed,我更新了顶部的脚本,以涵盖最小、完整和可验证。感谢您的帮助。脚本现在有 2 个组,我只想对名为组标题“电影”的组进行排序。(电影两边都有下划线)
0赞 Ed Morton 8/1/2018
您忘记添加预期的输出,请这样做以完成最小的可重现示例并避免任何误解。现在我什至不知道您是否希望输出或丢弃非电影组或其他什么,也不确定您希望对电影组进行排序。