Re: count line (217 Views)
Reply
Occasional Contributor
2ne1Abcd
Posts: 7
Registered: ‎09-11-2013
Message 1 of 4 (236 Views)

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

 

Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,736
Registered: ‎06-21-2000
Message 2 of 4 (217 Views)

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

 

 

Please use plain text.
Occasional Contributor
2ne1Abcd
Posts: 7
Registered: ‎09-11-2013
Message 3 of 4 (204 Views)

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

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,881
Registered: ‎03-06-2006
Message 4 of 4 (196 Views)

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.

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation