Count the occurrence of a string in an input file

2019-04-10 03:04发布

问题:

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

回答1:

The classic awk solution is something like:

$ awk 'NF{ count[ toupper( $0 ) ]++} 
    END{ for ( name in count ) { print name " appears " count[ name ] " times" };
}' input


回答2:

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/


回答3:

Assuming data.txt contains your word Following script will do.

while read line
do  
    uc=$(echo $line | tr [a-z] [A-Z] | tr -d ' ')
    echo  $uc $(grep -i "$uc" strs.txt | wc -l)
done< data.txt | sort | uniq

Output.

31
ALLEN 6
MARK 4
MOKADDIM 1
SHIPLU 1
TIM 4

Another option is

sort -f data.txt | uniq -i -c  | while read num word
do  
    echo $(echo $word|tr [a-z] [A-Z])  appeard  $num times
done

Note: I see your text file contains blank lines. So the 31 in the output contains the number of blank lines.



回答4:

for i in `sort filename |uniq -c``
do
    # --if to print data as u like--
done


标签: linux bash shell