Help with shell script and regular expressions

3

I'm doing this shell script code that should pass each line of a text file, and determine if the sentence is positive or negative depending on the IF condition

#!/usr/bin/ksh
file="${1}"

while read line;do

        pos=$(grep -oP ':\)|;\)|:D' $file | wc -l)   
        neg=$(grep -oP ':\('        $file | wc -l) 

if (( $pos * 2 > $neg * 5 )) ;then
   echo "Sentença Positiva "
else
   echo "Sentença Negativa"
fi

sleep 1
done <"$file"

But how much do I execute the script on top of this text file: test.txt

teste:) teste :) teste :) teste :)
teste:( teste :( teste :( teste :(

Instead of returning it:

Sentença Positiva
Sentença Negativa

it returns the two negatives:

Sentença Negativa
Sentença Negativa

How do I fix this problem?

    
asked by anonymous 12.11.2015 / 19:47

1 answer

0

You are iterating the file line by line with while read line;do , but you are applying grep to the whole file. The following modification should fix this problem:

pos=$(echo "$line" | grep -oP ':\)|;\)|:D' | wc -l)
neg=$(echo "$line" | grep -oP ':\('        | wc -l)
    
12.02.2016 / 16:10