Filter number of characters with SED or grep

Asked

Viewed 830 times

0

I have a file with strings I need at first filter for example only lines that has 5 characters And in a second moment filter only those that have at least 2 characters repeated together Example: display aazyx or zyaax but do not display azyxa And third filter lines q have at least one vowel.

That last one I know is possible, the other two I don’t know if it’s possible with sed. Thanks for your help and attention

  • In general, mathematically regular expressions have no memory, could not know what is behind. This is only possible using some modern meta characters (a little more detail here)

  • Now, it is possible to predict which are all possible cases of repetition, so could put in a large group of "or"s...

2 answers

3


I don’t know if I understand it very well, but suppose you have a text file in the following format:

txt file.

abcde
aabbc
kkkkk
ggggggggggggg
ccdde

We can filter as follows:

cat arquivo.txt | grep -x '.\{5,5\}' | grep -E '(.)\1{1}' | grep -E '[aeiouAEIOU]{1}'

Note 1: I think it would be better to use egrep instead of grep for regular expressions.

Obs 2: I am without time for the details, of a man grep to search for parameters.

  • thank you, simple and efficient

  • +1 or more compact mode: grep -xE '.{5}' arguivo.txt | grep -E '(.)\1' | grep -i '[aeiou]'

1

Hello, good afternoon.

I hope that’s what you need :)

#!/bin/bash
vCharsVogal=(aa ee ii oo uu)
vCharsConso=(bb cc dd ff gg hh ii jj kk ll mm nn pp qq rr ss tt vv xx zz ww yy)
vCharVogal=(a e i o u)
for line in $(cat $1); #//$1 eh o parametro para o arquivo de entrada 
do
        #1 -Primeiro testa se possui 5 chars 
        if (("${#line}" == "5"));
        then
                #2 - Depois verifica se possui repeticao de chars vogais
                continua=1
                for char in ${vCharsVogal[*]}
                do
                        countChar=$(echo "${line}" | awk -F"${char}" '{print NF-1}')
                        if (("$countChar" >= "1"));
                        then
                                #3 - Se entrou aqui, quer dizer que jah possui vogal :) 
                                echo "$line"
                                continua=0
                                break
                        fi
                done
                #Se nao teve nas vogais, ai faz as consoantes
                if(("${continua}" == "1"));
                then
                        for char in ${vCharsConso[*]}
                        do
                                countChar=$(echo "${line}" | awk -F"${char}" '{print NF-1}')
                                if (("$countChar" >= "1"));
                                then
                                        #3 - Vai verificar se existe vogal
                                        for vogal in ${vCharVogal[*]}
                                        do
                                                countVog=$(echo "${line}" | awk -F"${vogal}" '{print NF-1}')
                                                if (("$countVog" >= "1"));
                                                then
                                                        echo "$line"
                                                        continua=0
                                                        break
                                                fi
                                        done
                                        if(("${continua}" == "0"));
                                        then
                                                break
                                        fi
                                fi
                        done
                fi
        fi
done
  • thanks, the solution is good tbm although more complex than the @Tom Melo. However your solution will enable me tb other solution.

  • The code needs to be adapted a little to treat other characters. When I saw the solution of @Tom Melo, I confess that I wanted to remove my hahahha publication. The good thing is that we learn from both sides :) Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.