0

1st issue : My code is working only if grep take constant pattern like this :

echo "$s" | grep -oP '(?<=class="A3">).*(?=</a>)'

2nd issue : assigning output to a variable not working too

Here is my script :

#!/bin/sh

filename="data.txt"
Ptr_ValidChannel="><a title=\"Id: "
Ptr_ChannelNameStart="<class=\"A3\">"
Ptr_ChannelNameEnd="</a>"
while read -r line
do
    case "$line" in
        # working 100%
        #*$Ptr_ValidChannel*) echo "$line" | grep -oP '(?<=class="A3">).*?(?=</a>)' ;; 

        # not working
        #*$Ptr_ValidChannel*) echo $line | grep -oP '(?<=$Ptr_ChannelNameStart).*?(?=$Ptr_ChannelNameEnd)' ;;

        # not working
        *$Ptr_ValidChannel*) myvar=$(echo $line | grep -oP '(?<=$Ptr_ChannelNameStart).*?(?=$Ptr_ChannelNameEnd)') ;; 

    esac

done < "$filename"

echo $var_name

exit

To simplify things the data.txt content is :

    <TD WIDTH="15%"><a title="Id: I24 NEWS" class="A3">I24 News Français</a><br /><font color="#555555"> <a title="Sporadic or full 16/9 transmission"><img src="/169.gif"></a>

In my system the command :

 ls -la /bin/sh

output is :

  /bin/sh -> dash

best regards.

PS. NO BASH CODE PLEASE. ONLY SH.

user3072470
  • 131
  • 2
  • 11
  • 1
    Please read this : http://stackoverflow.com/a/1732454/867395 – Joan Charmant Dec 04 '14 at 09:00
  • Use an XML parser like `xmllint`. – Cyrus Dec 04 '14 at 09:01
  • I'm not looking to make an html parser, i just want to extract some data using a subset html tags. – user3072470 Dec 04 '14 at 09:45
  • second question resolved by putting the ? after the * quantifier : echo "$s" | grep -oP '(?<=class="A3">).*?(?=)' – user3072470 Dec 04 '14 at 09:49
  • Not very clear what you mean. Your `grep` works to me. To be more sure you don't catch the rest of the line, you can use `grep -oP '(?<=class="A3">)[^<]*(?=)'` – fedorqui Dec 04 '14 at 11:46
  • i want to pass the 2 variables patterns to grep and catch the output in a new variable for later use. *$Ptr_ValidChannel*) var_name=$(echo $line | grep -oP '(?<=$Ptr_ChannelNameStart).*?(?=Ptr_ChannelNameEnd)') ;; then var_name must contain the value "I24 News Français" – user3072470 Dec 04 '14 at 12:53
  • i modified the code to be more clear. – user3072470 Dec 04 '14 at 13:29

1 Answers1

0

After reading this article: dash as bin sh. i figured out what to do to make my code work correctly and more portable:

#! /bin/sh

filename='data.txt'
Ptr_ValidChannel='><a title="Id: '
Ptr_ChannelNameStart='class="A3">'
Ptr_ChannelNameEnd='</a>'
while read -r line
do
case "$line" in
    *"$Ptr_ValidChannel"*) var_name=$(printf %s "$line" | grep -oP '(?<='"$Ptr_ChannelNameStart"').*?(?='"$Ptr_ChannelNameEnd"')'); printf %s "$var_name"; printf '\n'; ;;
esac

done < "$filename"
exit

Thank you for your comments

best reagrds.

user3072470
  • 131
  • 2
  • 11