mercredi 30 juin 2021

Bash script, if statement in while loop, unwanted duplicate output

I'm doing a script to parse m3u files. The goal is to retrieve the variable tag and the url. I tested with this file.

#!/bin/bash

echo "name,tvg-id,tvg-name,tvg-country,group-title,languages,url"
while IFS= read -r line; do
    tags_detect="$(echo "$line" | grep -Eo '^#EXTINF:')"

    if [[ -n ${tag_detect} ]]; then
        get_chno="$(echo "$line" | grep -o 'tvg-chno="[^"]*' | cut -d '"' -f2)"
        get_id="$(echo "$line" | grep -o 'tvg-id="[^"]*' | cut -d '"' -f2)"
        get_logo="$(echo "$line" | grep -o 'tvg-logo="[^"]*' | cut -d '"' -f2)"
        get_grp_title="$(echo "$line" | grep -o 'group-title="[^"]*' | cut -d '"' -f2)"
        get_title="$(echo "$line" | grep -o ',[^*]*' | cut -d ',' -f2)"
        get_tvg_name="$(echo "$line" | grep -o 'tvg-name="[^"]*' | cut -d '"' -f2)"
        get_country="$(echo "$line" | grep -o 'tvg-country="[^"]*' | cut -d '"' -f2)"
        get_language="$(echo "$line" | grep -o 'tvg-language="[^"]*' | cut -d '"' -f2)"

        phrase="${get_title},${get_id},${get_tvg_name},${get_country},${get_grp_title},${get_language}"
    else
        url="$line"

    fi
    echo "${phrase},${url}"

done <"${1}"

So, Without "If" it works but i don't have url. I add a "IF" and ... :

,#EXTM3U
4 Turk Music,4TurkMusic.fr,4 Turk Music,FR;TK,Music,Turkish,#EXTM3U
4 Turk Music,4TurkMusic.fr,4 Turk Music,FR;TK,Music,Turkish,http://51.210.199.30/hls/stream.m3u8
Alpe d’Huez TV,AlpedHuezTV.fr,Alpe d’Huez TV,FR,,French,http://51.210.199.30/hls/stream.m3u8
Alpe d’Huez TV,AlpedHuezTV.fr,Alpe d’Huez TV,FR,,French,https://edge10.vedge.infomaniak.com/livecast/ik:adhtv/chunklist.m3u8

... It's broken and I don't found my error.

desired output:

4 Turk Music,4TurkMusic.fr,4 Turk Music,FR;TK,Music,Turkish,http://1.2.3.4/hls/stream.m3u8
Alpe d’Huez TV,AlpedHuezTV.fr,Alpe d’Huez TV,FR,,French,https://edge10.vedge.infomaniak.com/livecast/ik:adhtv/chunklist.m3u8

I don't understand my mistake.

Aucun commentaire:

Enregistrer un commentaire