Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the occurrence of a string in an input file

Tags:

linux

bash

shell

There is a shell script which is supposed to process an incoming text file.

This text file contains strings split on multiple lines, and each string is present more than once.

The shell script needs to read this text file and output the String and count of each string.

Consider the text file is:

Tim

tim

Mark

MARk

Allen

ALLen

allEN

The output should be like this:

Tim appears 2 times

Mark appears 2 times

Allen appears 3 times

Right now, I am able to print the occurrence of strings, but that gets repeated the number of times the string occurs, that is "Tim appears 2 times" gets printed twice. I was trying to replace a string with NULL as soon as I count its occurrence, but for some reason, the sed is not working, coz maybe I am not invoking it at the right place (or in right way)

 #!/bin/bash

INPUT_FILE="$1"
declare -a LIST_CHARS

if [ $# -ne 1 ]
then
        echo "Usage: $0 <file_name>"
        exit 1
fi


if [ ! -f $INPUT_FILE ]
then
        echo "$INPUT_FILE does not exists. Please specify correct file name"
        exit 2
fi

while read line
do
        while read i
        do
                echo $line
                count=`grep -i $line | wc -l`
                echo "String $line appears $count times"
        done < $INPUT_FILE

done < $INPUT_FILE
like image 274
Incognito Avatar asked Nov 29 '22 03:11

Incognito


2 Answers

The classic awk solution is something like:

$ awk 'NF{ count[ toupper( $0 ) ]++} 
    END{ for ( name in count ) { print name " appears " count[ name ] " times" };
}' input
like image 29
William Pursell Avatar answered Dec 06 '22 08:12

William Pursell


You can also use sort and uniq with flags to ignore case:

sort -f FILE | uniq -ic

Simple sed command can change the output format to the specified one:

s/^ *\([0-9]\+\) \(.*\)/\2 appears \1 times/
like image 173
choroba Avatar answered Dec 06 '22 07:12

choroba