- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: count line
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2013 01:30 AM
10-03-2013 01:30 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2013 06:48 AM
10-03-2013 06:48 AM
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
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2013 08:37 PM
10-03-2013 08:37 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2013 01:04 AM
10-04-2013 01:04 AM
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.