Need your help for this scripting issue I have. I am not really good at this, so seeking your help.
I have a file looking similar to this:
Hello, i am human and name=ABCD.
How are you?
Hello, i am human and name=PQRS.
I am good.
Hello, i am human and name=ABCD.
Good bye.
Hello, i am human and name=XYZ.
Now I need to search "human" keyword within this file and then list out the values after "name=" and also the count of common values.
i.e. output would be:
Count of "human"=4
Count for name "ABCD"=2
Count for name "PQRS"=1
Count for name "XYZ"=1
Please help me how to do this.
I can get the lines having "human" with this and the total count of 'human':
Could you please use the following script for same.
count_xyz=0
count_pqrs=0
count_abcd=0
abcd="ABCD"
xyz="XYZ"
pqrs="PQRS"
icount_human=0
count_human=0
while read line
do
check_human=`echo $line | grep "human"`
name_check=`echo $line | grep "name"`
value_name_check=`echo $name_check | awk -F"=" '{print$2}' | grep -v '^$' | cut -f1 -d.`
if [[ -n ${check_human} ]]
then
let "count_human = count_human + 1"
fi
if [[ "$value_name_check" == "$abcd" ]]
then
let "count_abcd = count_abcd + 1"
fi
if [[ "$value_name_check" == "$pqrs" ]]
then
let "count_pqrs = count_pqrs + 1"
fi
if [[ "$value_name_check" == "$xyz" ]]
then
let "count_xyz = count_xyz + 1"
fi
done < "requirement_check_count"
echo "Count for human is=" $count_human
echo "Count for ABCD=" $count_abcd
echo "Count for PQRS=" $count_pqrs
echo "Count for XYZ=" $count_xyz
Output will be as follows then.
$ ksh check_requirement_check_count.ksh
Count for human is= 4
Count for ABCD= 2
Count for PQRS= 1
Count for XYZ= 1
Thanks for your reply. I missed mentioning one info that is the values after "name=" are not static/fixed but can change/dynamic. I used ABCD/PQRS/XYZ as examples only.
Its like extracting the string after "name=" for all lines having "human" and then count the total occurances for each value of "name="
Little modification in CarloM code (used match function and pass variable to awk)
awk -v var="human" '/human/{count++; match($0,/=(.+?)$/) ; a=substr($0,RSTART+1,RLENGTH-2);array[a]++} END { print "count of "var"="count; for (i in array) {print "count of name "i "="array;} }' filename
Hi ctsgnb,
Kindly please explain the command you have pasted. It gives the output without formatting - its fine.
Also if the line would have been ...
Hello, i am human and name="ABCD" now.
$ awk -F'[ ,=]' '/^Hello/{for(i=1;i<=NF;i++)A[$i]++}END{for (i in A)if(i!="")print "count of " "\""i"\"""=" OFS A}' OFS=\\t file
count of "i"= 4
count of "XYZ."= 1
count of "and"= 4
count of "name"= 4
count of "Hello"= 4
count of "ABCD."= 2
count of "am"= 4
count of "PQRS."= 1
count of "human"= 4
if you want to count only few fields you can do like this
$ awk -F'[ ,=]' '/^Hello/{for(i=1;i<=NF;i++){if(($i~"ABCD")||($i~"PQRS")||($i~"XYZ")||($i~"human"))A[$i]++}}END{for (i in A)print "count of " "\""i"\"""=" OFS A}' OFS=\\t file
count of "XYZ."= 1
count of "ABCD."= 2
count of "PQRS."= 1
count of "human"= 4