Extract information from a text file with shell script and regular expressions

0

I want to make a script with a shell script that extracts from an emoticons text file, for example ;) , :) , :3 , :( , xD and also count the emoticons of each sentence. A statement is declared positive if the sum of the positive emoticons exceeds 2.5 times the sum of the negative emoticons if present, I made this code that counts the amount of positive, please how would I end this script?

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

while IFS= read -r line
x=0
do
    let "x=x+1"

    qtd=$(echo "$line" | sed -r 's/ /\n/g' | grep "POSITIVO" | wc -l)
    echo "SENTENÇA $x \t - POSITIVO - $qtd"
done <"$file"
    
asked by anonymous 09.11.2015 / 20:53

1 answer

1

I do not know if I completely understand the request ...

The example below calculates the polarity of a file.

Actually this is bash but with minor changes it should work like ksh ...

#!/bin/bash
pos=$(grep -oP ':\)|;\)|:D' $1 | wc -l)     ## juntar outros positivos
neg=$(grep -oP ':\('        $1 | wc -l)     ## juntar outros negativos

if (( $pos * 2 > $neg * 5 )) ;then
   echo "Positivo "
else
   echo "Negativo"
fi

UPDATE : To give the polarity of each line we can for example join with the version proposed by the PO:

#!/bin/bash

while read line
do
  pos=$(<<<$line grep -oP ':\)|;\)|:D' | wc -l)   ## juntar outros positivos
  neg=$(<<<$line grep -oP ':\('        | wc -l)   ## juntar outros negativos

  echo $line $pos $neg
  if (( $pos * 2 > $neg * 5 )) ;then
     echo "Positivo "
  else
     echo "Negativo"
  fi

done <"$1"
    
09.11.2015 / 23:12