Sorting 2 lines at a time with sed

awk multiple lines into one
msort
linux sort blocks of text

I would like to use sed to sort a large playlist file. Each playlist item has 2 lines so I need to sort based on the string after tvg-name= in the first line (so in this case line one would be UK: Button 1(SG)) sorting this first line and the following line with it (the one that begins with http).

I've seen very similar regex examples but the syntax is beyond me to reverse engineer to fit my file. Can anyone help?

#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

Expected Ouput :

#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

I copied and pasted a section of the movies from my master and neither of the scripts make a change to the output. Example Below :

Input :

#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

Expected Ouptut would be the Movie Item "Fracture" at the top due to alpabeticalised sort.

#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

If you want to do this with a tool like sed, you must either A) have faith that your fields don't contain anything malignant like tvg-id="http://..., or B) write extremely painstaking scripts.

I would try something crude but effective, like this. First, combine two lines into one:

sed 'N;s/\n//'

Then copy the tvg-name field to the front of the line:

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

Then sort:

sort

Then remove the field I'd added:

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

Then split the one line back into two:

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

Putting it all together:

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

Sorting 2 lines at a time with sed, If you want to do this with a tool like sed, you must either A) have faith that your fields don't contain anything malignant like tvg-id="http:// , or B)  Sorting 2 lines at a time with sed. I would like to use sed to sort a large playlist file. Each playlist item has 2 lines so I need to sort based on the string after tvg-name= in the first line (so in this case line one would be UK: Button 1(SG)) sorting this first line and the following line with it (the one that begins with http).


This might work for you (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

Convert each two line record into a single line by duplicating the first line and removing the newline between the first and second lines. Gather the entire file up into memory held in the hold space. At the end of file, swap to the hold space and using GNU sed's evaluate command as a flag in the substitution command, sort the file on the third field in the single line record. Reformat the single line record back into a two line record and print the results.

Sort text files with multiple lines as a row, The sed command prepends a newline to every #+ line except for line 1. -w sorts the whole record (lexicographically). There are options for  The "Date Time" column is to be used as an index to match up all 13+ files and produce a "Master" File (if you will) with only 1 "Date Time" column (preferable 1st col) The issue is all files have some "Date Time" data missing, so 10 might have "7/22/2011 15:03" but other wont.


Without better sample input and the expected output it's a guess but this might be what you're trying to do:

$ 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

or if you wanted it sorted in reverse:

$ 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

It just prefixes each line with the key value from between the quotes followed by a count of the lines starting at that value, then sorts it by those added fields, then removes the added fields again. It's easiest to understand if you look at how it works in stages:

1) Add the 2 prefix fields to be used for sorting:

$ 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) Sort the output using the 2 fields added at step 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
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) Remove the 2 fields added at step 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

Note that the above will work for any number of lines in multi-line blocks and the same approach works for sorting on multiple different substrings. It's also very efficient (stores almost nothing in memory) and easy to debug/enhance/maintain later.

Edit 1: Based on your comments below and the new sample input you provided the following MY be what you want but I don't know if it is or not since the didn't provide the expected output and best I can tell the sample input you provided is already sorted so there's no way to tell if the tool is doing anything at all.

$ 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

Edit 2: Given the updated sample input and expected output now posted in your question, this may be what you want instead:

$ 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

The above script is slightly more complicated than it has to be so it'd be trivial for you to add additional "group-title"s to sort on all in the same invocation of the tool if/when you need to do that in future by just tweaking the if ( groupTitle == "_Movies_" ) line.

sort multi-line blocks with AWK, We encountered a way to use the sort command, which sorts lines, to sort done by reducing each block of multiple lines into a single one, applying sort to those, Then the third, and so on one at a time to grasp what each stage adds toward  Type the following head command to display first 20 lines of a file named “bar.txt”: head -20 bar.txt. head -20 bar.txt. How to view the beginning of text file with head command. Pass the -n option, as follows to control the Length of output: head -n 5 /etc/passwd. You can view multiple files too: head file1 file2 file3.


SED command in Linux, Sed use Patter buffer when it read files, line by line and that currently read line is 7 – Delete the lines which matches the pattern and 2 lines after to that – The above command will get and print the consecutive 4 lines along with the current line because it was fed into a for loop.The search pattern $2=="KFC" will helps to get a particular line from the multiple lines.


Sed Command in Linux/Unix with examples, Searching Algorithms · Sorting Algorithms · Graph Algorithms · Pattern Though most common use of SED command in UNIX is for substitution or for find and replace. Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc flags to rows generated by the /p flag and prints the replaced lines only one time. in the above example, i need a sed command that would take inputs from file2, 1 line at a time, delete email1@email.com and email2@email2.com from file1 and output the result to file3. This may be a better description of what you want to do, if you mean "email addresses" when you say mails and you provided a represented sample of lines from


Text Processing Commands, 3 This line occurs three times. bash$ sort testfile | uniq -c | sort -nr 3 This line occurs 2) As it stands, this script gives "false positives" for #+ Perl, awk, and other