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
Assuming
data.txt
contains your word Following script will do.Output.
Another option is
Note: I see your text file contains blank lines. So the 31 in the output contains the number of blank lines.
You can also use sort and uniq with flags to ignore case:
Simple
sed
command can change the output format to the specified one:The classic awk solution is something like: