1752572 Members
4530 Online
108788 Solutions
New Discussion

Re: count line

 
2ne1Abcd
Occasional Contributor

count line


I would like to have script to do it , please help .

 

There is a log file , the sample is as below .

#vi log.txt
xxxx111xxxxxxxaaaxxx
xxxx111xxxxxxxxxbbbx
xxxxxxxxxxxxxxcccxxx
xxx111xxxxxxxxxxxxxx
xxxx111xxxxxxxaaaxxx
xxxx111xxxxxxxxxbbbx
xxxx111xxxxxxxcccxxx


I want to count the occurence of a specific string in the log file .


the condition is where "111" AND the specific string ( eg. aaa , bbb , ccc ) is exist , then count how many lines have this two string at the same time .

 

To better for maintainence , there is a file to keep the string , the file content is as below .
#vi string.txt
aaa
bbb
ccc

 

As above , therefore ,  the result should be as below.

there are 2 lines have both "111" and "aaa"
there are 2 lines have both "111" and "bbb"
there are 1 lines have both "111" and "ccc"

 

explaination
=============

xxxx111xxxxxxxaaaxxx  ==> both "111" and "aaa" exist , count 1
xxxx111xxxxxxxxxbbbx  ==> both "111" and "bbb" exist , count 1  
xxxxxxxxxxxxxxcccxxx  ==> only "ccc" exist
xxx111xxxxxxxxxxxxxx  ==> only "111" exist
xxxx111xxxxxxxaaaxxx  ==> both "111" and "aaa" exist , count 1
xxxx111xxxxxxxxxbbbx  ==> both "111" and "bbb" exist , count 1
xxxx111xxxxxxxcccxxx  ==> both "111" and "ccc" exist , count 1

 

therefore , we got the result

 

there are 2 lines have both "111" and "aaa"
there are 2 lines have both "111" and "bbb"
there are 1 lines have both "111" and "ccc"

 

would advise how to write it ? thanks

 

3 REPLIES 3
Patrick Wallek
Honored Contributor

Re: count line

Have a look at this script:

 

# cat script.sh
#!/usr/bin/sh
for LTR in $(< string.txt)
do
COUNT=$(grep 111 log.txt | grep -c ${LTR})
echo "There are ${COUNT} lines with both 111 and ${LTR}"
done

 

 

# ./script.sh
There are 2 lines with both 111 and aaa
There are 2 lines with both 111 and bbb
There are 1 lines with both 111 and ccc

 

 

2ne1Abcd
Occasional Contributor

Re: count line

thanks reply ,

I have an additional request , if the string.txt is as below , there is another column in the file , seperate by space

#vi string.txt
aaa   first line
bbb   second line
ccc   third line


if I would like the output is as below ( display the string of second column ) , how to make it ?

There are 2 lines with both 111 and first line
There are 2 lines with both 111 and second line
There are 1 lines with both 111 and third line


Another question , if the no. of character in the column is different , if I use echo to change the display format to show the ${COUNT} output , then the output will not tidy due to the different no. of character

for example ,

#vi script.sh
for LTR in $(< string.txt)
do
COUNT=$(grep 111 log.txt | grep -c ${LTR})
second_column=...
echo ${second_column} ${COUNT}
done


The output will be as below , as the no of character of second column is different , then the ${COUNT} is not tidy to display, how to make sure the ${COUNT} column is align ?
#script.sh
first line          4
second line   5
third line                            2

Dennis Handly
Acclaimed Contributor

Re: count line

>if the string.txt is as below, there is another column in the file

>if I would like the output is as below (display the string of second column)

 

What did you want to do with the second column?  Just print it but still search for the first column?

 

while read LTR second; do
   COUNT=$(grep 111 log.txt | grep -c ${LTR})
   echo "There are ${COUNT} lines with both 111 and ${second}"
done < string.txt

 

> if I use echo to change the display format to show the ${COUNT} output , then the output will not tidy

 

You can do several things.  Put the count before the varying char column:

   echo "${COUNT} ${second_column}"

And if you think the number of chars in $COUNT will vary, you can  use typeset:

    typeset -R5 COUNT

 

That way COUNT is always 5 wide and the number is right justified.

 

You can also use typeset on second_column to set a fixed width:

   typeset -L20 second_column

 

You can also do the same things with printf(1):

   printf "%-20.20s %5d\n" $second_column $COUNT

 

This left justifies second_column in a 20 character field and truncates if longer than 20.

COUNT is right justified in a 5 character field.