Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!