Jump to content


Photo

String parsing


  • Please log in to reply
12 replies to this topic

#1 doglover

  • Rytec EPG Team
  • 17,006 posts

+638
Excellent

Posted 12 August 2020 - 09:40

Can some-one help me parsing this in pure bash:

#EXTINF:-1 tvg-chno="729" tvg-id="Pluto TV Kids" tvg-name="Pluto TV Kids" tvg-logo="https://images.pluto.tv/channels/51c75f7bb6f26ba1cd00002f/colorLogoPNG.png" group-title="PlutoTV" group-title="Kids",Pluto TV Kids

Into separate values of each element.  I want to do this in pure bash.  (utils like grep and sed are allowed)

I searched around and found some solutions.  But these are based on the position of each element.  So if the order is changed, I can rewrite the stuff.

 

Ideally, I would like to have separate variables which contains the value.

So variables:

 

tvg-id

tvg-chno

tvg-logo

group-title (2 values)

 

A regex expression:  tvg-id="(.*?)"

would do the job.  Only how can I use this in a bash script?


Edited by doglover, 12 August 2020 - 09:46.

~~Rytec Team~~
Maxytec Multibox SE OpenPli (used as mediaplayer)
Mutant HD2400 OpenPli
Vu+ Duo OpenPli (backup)

Synology NAS

Sat: 13E, 19.2E, 23.5E and 28.2E
*Pli/Rytec EPG POWERED*


Re: String parsing #2 ims

  • PLi® Core member
  • 13,623 posts

+212
Excellent

Posted 12 August 2020 - 11:31

If you want parse strings between " ", you can easy use "cut" command in script

 

f.eg. your line in a.txt

echo `cut -d "=" -f2 a.txt | cut -d "\"" -f2`
echo `cut -d "=" -f3 a.txt | cut -d "\"" -f2`
echo `cut -d "=" -f4 a.txt | cut -d "\"" -f2`
echo `cut -d "=" -f5 a.txt | cut -d "\"" -f2`
echo `cut -d "=" -f6 a.txt | cut -d "\"" -f2`
echo `cut -d "=" -f7 a.txt | cut -d "\"" -f2`

you will get

729
Pluto TV Kids
Pluto TV Kids
https://images.pluto.tv/channels/51c75f7bb6f26ba1cd00002f/colorLogoPNG.png
PlutoTV
Kids

Note - if last part in your line is ok  (is not whole between " ") then:

echo `cut -d "=" -f2 a.txt | cut -d "\"" -f2`
echo `cut -d "=" -f3 a.txt | cut -d "\"" -f2`
echo `cut -d "=" -f4 a.txt | cut -d "\"" -f2`
echo `cut -d "=" -f5 a.txt | cut -d "\"" -f2`
echo `cut -d "=" -f6 a.txt | cut -d "\"" -f2`
echo `cut -d "=" -f7 a.txt`
729
Pluto TV Kids
Pluto TV Kids
https://images.pluto.tv/channels/51c75f7bb6f26ba1cd00002f/colorLogoPNG.png
PlutoTV
"Kids",Pluto TV Kids


Note - may be, some version wants space between -f and number ... -f 2


Edited by ims, 12 August 2020 - 11:34.

Kdo nic nedělá, nic nezkazí!

Re: String parsing #3 doglover

  • Rytec EPG Team
  • 17,006 posts

+638
Excellent

Posted 12 August 2020 - 11:51

That is the method I know, but it depends on the position....

Which is what I am trying to avoid.


Edited by doglover, 12 August 2020 - 11:51.

~~Rytec Team~~
Maxytec Multibox SE OpenPli (used as mediaplayer)
Mutant HD2400 OpenPli
Vu+ Duo OpenPli (backup)

Synology NAS

Sat: 13E, 19.2E, 23.5E and 28.2E
*Pli/Rytec EPG POWERED*


Re: String parsing #4 doglover

  • Rytec EPG Team
  • 17,006 posts

+638
Excellent

Posted 12 August 2020 - 15:32

Tried something:

#!/bin/bash
substring () {
    string=$1
    fromstring=${string#*$2}
    fromstring=${fromstring#*\"}
    tostring=${fromstring#*$3}
    value=${fromstring/"$tostring"/}
    value=${value/"$3"/}
    value=${value/"$2"/}
    value=${value//=/}
    value=${value//\"/}   
    echo $value
}
line='#EXTINF:-1 tvg-chno="729" tvg-id="Pluto TV Kids" tvg-name="Pluto TV Kids" tvg-logo="https://images.pluto.tv/channels/51c75f7bb6f26ba1cd00002f/colorLogoPNG.png" group-title="PlutoTV" group-title="Kids",Pluto TV Kids'
func_result=$(substring "$line" "tvg-chno=" "tvg")
echo $func_result
func_result=$(substring "$line" "group-title=" ",")
echo $func_result

Used a function which i can call to obtain several values by defining the start and end strings.


Edited by doglover, 12 August 2020 - 15:33.

~~Rytec Team~~
Maxytec Multibox SE OpenPli (used as mediaplayer)
Mutant HD2400 OpenPli
Vu+ Duo OpenPli (backup)

Synology NAS

Sat: 13E, 19.2E, 23.5E and 28.2E
*Pli/Rytec EPG POWERED*


Re: String parsing #5 ims

  • PLi® Core member
  • 13,623 posts

+212
Excellent

Posted 12 August 2020 - 15:49

but it depends on position too ...


Kdo nic nedělá, nic nezkazí!

Re: String parsing #6 ims

  • PLi® Core member
  • 13,623 posts

+212
Excellent

Posted 12 August 2020 - 16:52

Problem are spaces in strings ...

 

You can try this:

#find="#EXTINF:-1"
#replace=""
line=${line//$find/$replace}
find=" tvg-"
replace="!tvg-"
line=${line//$find/$replace}
find=" group-title"
replace="!group-title"
line=${line//$find/$replace}
OLDIFS=$IFS
IFS='!'
read -ra ADDR <<< "$line"
echo "---------------------"
for i in ${ADDR[@]}; do
    echo "$i"
done
echo "---------------------"
IFS=$OLDIFS

output:

---------------------
#EXTINF:-1
tvg-chno="729"
tvg-id="Pluto TV Kids"
tvg-name="Pluto TV Kids"
tvg-logo="https://images.pluto.tv/channels/51c75f7bb6f26ba1cd00002f/colorLogoPNG.png"
group-title="PlutoTV"
group-title="Kids",Pluto TV Kids
---------------------

then it is better for work, imho
 

 


Kdo nic nedělá, nic nezkazí!

Re: String parsing #7 ims

  • PLi® Core member
  • 13,623 posts

+212
Excellent

Posted 12 August 2020 - 18:21

try it

line=${line//" tvg-"/"!+!tvg-"}
line=${line//" group-title"/"!+!group-title"}
OLDIFS=$IFS
IFS=!+!
read -ra ADDR <<< "$line"
echo "---------------------"
second_group_title=false
for i in ${ADDR[@]}; do

	if [ -n "$i" ];then
		v=${i#*=}
		if [ "${i%%=*}" = "tvg-chno" ];then
			tvg_chno=${v//\"/}   
		elif [ "${i%%=*}" = "tvg-id" ];then
			tvg_id=${v//\"/}   
		elif [ "${i%%=*}" = "tvg-name" ];then
			tvg_name=${v//\"/}   
		elif [ "${i%%=*}" = "tvg-logo" ];then
			tvg_logo=${v//\"/}   
		elif [ "${i%%=*}" = "group-title" ]; then
			if ! ( $second_group_title );then
				group_title=${v//\"/}
				second_group_title=true
			else
				group_title1=${v//\"/}
			fi
		fi
	fi
done
echo "---------------------"
IFS=$OLDIFS
echo $tvg_chno
echo $tvg_id
echo $tvg_name
echo $tvg_logo
echo $group_title
echo $group_title1

 


Kdo nic nedělá, nic nezkazí!

Re: String parsing #8 doglover

  • Rytec EPG Team
  • 17,006 posts

+638
Excellent

Posted 13 August 2020 - 14:21

Does not work on an enigma2 receiver:

root@hd2400:/etc/epgimport# ./Nieuw2.sh
---------------------
./Nieuw2.sh: line 8: syntax error near unexpected token `$'do\r''
'/Nieuw2.sh: line 8: `for i in ${ADDR[@]}; do
root@hd2400:/etc/epgimport#

 


~~Rytec Team~~
Maxytec Multibox SE OpenPli (used as mediaplayer)
Mutant HD2400 OpenPli
Vu+ Duo OpenPli (backup)

Synology NAS

Sat: 13E, 19.2E, 23.5E and 28.2E
*Pli/Rytec EPG POWERED*


Re: String parsing #9 WanWizard

  • PLi® Core member
  • 68,544 posts

+1,737
Excellent

Posted 13 August 2020 - 14:25

Did you install bash?

 

The default ash shell in busybox doesn't support a lot of bash features...


Currently in use: VU+ Duo 4K (2xFBC S2), VU+ Solo 4K (1xFBC S2), uClan Usytm 4K Pro (S2+T2), Octagon SF8008 (S2+T2), Zgemma H9.2H (S2+T2)

Due to my bad health, I will not be very active at times and may be slow to respond. I will not read the forum or PM on a regular basis.

Many answers to your question can be found in our new and improved wiki.


Re: String parsing #10 doglover

  • Rytec EPG Team
  • 17,006 posts

+638
Excellent

Posted 13 August 2020 - 16:29

Sorry.  Fixed it.

bash was not defined properly.

 

Yes bash was installed.  But I did not define it correctly at the top of script.

 

PS:  I am making progress with w\hat I am intending to do.

(parsing m3u files for use on enigma, including EPG)


Edited by doglover, 13 August 2020 - 16:32.

~~Rytec Team~~
Maxytec Multibox SE OpenPli (used as mediaplayer)
Mutant HD2400 OpenPli
Vu+ Duo OpenPli (backup)

Synology NAS

Sat: 13E, 19.2E, 23.5E and 28.2E
*Pli/Rytec EPG POWERED*


Re: String parsing #11 Pr2

  • PLi® Contributor
  • 6,070 posts

+257
Excellent

Posted 13 August 2020 - 19:09

Use awk this is very a very powerful tool.

 

Or you can also use sed, you copy the string several time and you replace the the value you want to extract:

 

sed -e 's/.*tvg-id="//g' this clean up everything before tvg-id

sed -e 's/".*"//g' this will clean up all the rest after the value you want

The result will be the value you expected regardless of the tag position.

 

Pr2


Edited by Pr2, 13 August 2020 - 19:12.

NO SUPPORT by PM, it is a forum make your question public so everybody can benefit from the question/answer.
If you think that my answer helps you, you can press the up arrow in bottom right of the answer.

Wanna help with OpenPLi Translation? Please read our Wiki Information for translators

Sat: Hotbird 13.0E, Astra 19.2E, Eutelsat5A 5.0W
VU+ Solo 4K: 2*DVB-S2 + 2*DVB-C/T/T2 (used in DVB-C) & Duo 4K: 2*DVB-S2X + DVB-C (FBC)

AB-Com: PULSe 4K 1*DVB-S2X (+ DVB-C/T/T2)
Edision OS Mio 4K: 1*DVB-S2X + 1*DVB-C/T/T2
 


Re: String parsing #12 doglover

  • Rytec EPG Team
  • 17,006 posts

+638
Excellent

Posted 15 August 2020 - 08:26

Another solution:

line='#EXTINF:-1 tvg-chno="729" tvg-id="Pluto TV Kids" tvg-name="Pluto TV Kids" tvg-logo="https://images.pluto.tv/channels/51c75f7bb6f26ba1cd00002f/colorLogoPNG.png" group-title="PlutoTV" group-title="Kids",Pluto TV Kids'
    if [[ "$line" == *"tvg-chno"* ]]; then
        SID=${line##*tvg-chno=\"}
        SID=${SID%%\"*}
    fi
    if [[ "$line" == *"tvg-id"* ]]; then   
        tvg_id=${line##*tvg-id=\"}
        tvg_id=${tvg_id%%\"*}
    fi
    if [[ "$line" == *"tvg-something"* ]]; then
        tvg_name=${line##*tvg-something=\"}
        tvg_name=${tvg_name%%\"*}
    fi
echo $SID
echo $tvg_id
echo $tvg_name

Tested an not existing tag (for in case it is missing).  And it gives the expected result - nothing.
 


Edited by doglover, 15 August 2020 - 08:28.

~~Rytec Team~~
Maxytec Multibox SE OpenPli (used as mediaplayer)
Mutant HD2400 OpenPli
Vu+ Duo OpenPli (backup)

Synology NAS

Sat: 13E, 19.2E, 23.5E and 28.2E
*Pli/Rytec EPG POWERED*


Re: String parsing #13 ozzsurf

  • Senior Member
  • 131 posts

+3
Neutral

Posted 15 August 2020 - 23:59

bash

grep -o 'tvg-chno="[^"]*' 1 > 2  ;grep -o 'tvg-id="[^"]*' 1 >>2 ;grep -o 'tvg-logo="[^"]*' 1 >>2 ;grep -o 'group-title="[^"]*' 1 >>2 ;sed -i 's/tvg-chno="//g' 2 ;grep -o 'tvg-id="[^"]*' 1 ;sed -i 's/tvg-id="//g' 2 ;grep -o 'tvg-logo="[^"]*' 2 ;sed -i 's/tvg-logo="//g' 2 ;sed -i 's/group-title="//g' 2

output

729
Pluto TV Kids
https://images.pluto.tv/channels/51c75f7bb6f26ba1cd00002f/colorLogoPNG.png
PlutoTV
Kids

 


Edited by ozzsurf, 16 August 2020 - 00:00.



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users