Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
Obtaining the total of coincidences with multiple pattern using grep command

I have a file in Linux contains strings:

CALLTMA
Starting
Starting
Ending
Starting
Ending
Ending
CALLTMA
Ending

I need the quantity of any string (FE. #Ending, # Starting, #CALLTMA). In my example I need obtaining:

CALLTMA : 2
Starting: 3
Ending : 4

I can obtaining this output when I execute 3 commands:

grep -i "Starting" "/myfile.txt" | wc -l
grep -i "Ending" "/myfile.txt" | wc -l
grep -i "CALLTMA" "/myfile.txt" | wc -l

I want to know if it is possible to obtain the same output using only one command.

I try running this command

grep -iE "CALLTMA|Starting|Ending" "/myfile.txt" | wc -l

But this returned the total of coincidences. I appreciate your help .

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Use sort and uniq:

sort myfile.txt | uniq -c

The -c adds the counts to the unique lines. If you want to sort the output by frequency, add

| sort -n

to the end (and change to -nr if you want the descending order).

over 4 years ago · Santiago Trujillo Denunciar

0

A simple awk way to handle this:

awk '{counts[$1]++} END{for (c in counts) print c, counts[c]}' file

Starting 3
Ending 4
CALLTMA 2
over 4 years ago · Santiago Trujillo Denunciar

0

grep -c will work. You can put it all together in a short script:

for i in Starting CALLTMA Ending; do 
    printf "%-8s  : %d\n" "$i" $(grep -c "$i" file.txt)
done

(to enter the search terms as arguments, just use the arguments array for the loop list, e.g. for i in "$@"; do)

Output

Starting  : 3
CALLTMA   : 2
Ending    : 4
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda