LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-19-2008, 08:25 AM   #1
simpi
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Rep: Reputation: 0
Unhappy counting script


Hi I'm fighting with script that can count lines with particular text in it.

To be concrete:
I need to open file and count lines which starts with @08:26:32 and contains [>>]

lines looks like:
@08:26:32.2044 [>>] 08 00 00 1C 08 02 00 28 64 96 1C 14 91 A1 11 02 01 03 02 01 8A 40 09 96 49 06 8C 31 39 30 30 B6
@08:26:32.2044 [gctmi] Distributing request SysRequestRegister
@08:26:32.2044 [asai] Address [19507,tRP,sLNK] (sysReqRegisterAddress)
TP_AsatData
REGISTER CWV:29
Facility: INVOKE
InvokeId: 3
Operation: EventNotification

the first data is time and I need to count lines in particular second which contains [>>].

I know it is simple, but I tride to write it and it always writes me some error message

thank you
 
Old 08-19-2008, 08:38 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
I know it is simple, but I tride to write it and it always writes me some error message
Why don't you show us what you tried and what the error message was???

Hint:
Something like this:
grep <pattern> filename | wc <option>

With grep, the trick is to define <pattern> to be sure it does the right thing for all cases.

"man wc" to see the options for counting lines.
 
Old 08-19-2008, 08:44 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can count matching lines using the -c option of grep, for example
Code:
grep ^@08:26:32 file | grep -c [\>\>]
 
Old 08-19-2008, 08:52 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
OR:
grep -c "^@08:26:32.*>>" filename
 
Old 08-19-2008, 09:35 AM   #5
simpi
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Thank you both guys.
 
Old 08-19-2008, 10:03 AM   #6
simpi
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Original Poster
Rep: Reputation: 0
weird is that when I just use command

cat log|grep -c "^@08:26:43.*>>"

it has different results than this command

cat log|grep ^@08:26:43 | grep -c [\>\>]


I found that the second one counts this line too:
@08:26:43.8470 [ISCC] Debug: Translate: '606167470' -> ''; result 1 ()
I don't know why because it doesn't contain [\>\>]

I'm really confused
 
Old 08-19-2008, 10:10 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Really strange. Following the last post by pixellany you can refine his pattern using:
Code:
grep "^@08:26:32..... \[>>\]" log
This will grep for @08:26:32 at the beginning of the line, followed by 5 characters, a blank space and finally [>>]. Anyway I'm trying to reproduce your behavior on my system, but I can't get it.
 
Old 08-19-2008, 10:22 AM   #8
simpi
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Original Poster
Rep: Reputation: 0
this command do the same like command with *>> so it probably works.

You can test it on this parragraph:

@08:26:32.2044 [>>] 08 00 00 1C 08 02 00 28 64 96 1C 14 91 A1 11 02 01 03 02 01 8A 40 09 96 49 06 8C 31 39 30 30 B6
@08:26:32.2044 [gctmi] Distributing request SysRequestRegister
@08:26:32.2044 [asai] Address [19007,tRP,sUNK] (sysReqRegisterAddress)
sex P_AsaiData
REGISTER CRV:29
Facility: INVOKE
InvokeId: 3
Operation: EventNotification
Domain: (VDN),'19007'
REGISTER CRV:8bc
Facility: INVOKE
InvokeId: 3
Operation: Domain_Control
Domain: (Extension),'6578'
@08:26:43.8470 [ISCC] Debug: Translate: '606167470' -> ''; result 1 ()
@08:26:43.9381 [>>] 08 00 00 1B 08 02 08 BC 64 96 1C 13 91 A1 10 02 01 03 02 01 C4 40 08 96 49 05 83 36 35 37 B8
message RequestRegisterAddress

and here are my results for it:

simon@stepan:~/log$ cat testlog2|grep ^@08:26:43 | grep -c \[\>\>\]
2
simon@stepan:~/log$ cat testlog2|grep -c "^@08:26:43.*>>"
1

The original log where I work with this command has more than 150MB so it is not possible controll if it is correct
 
Old 08-19-2008, 10:33 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
I think it is a matter of escaping special characters, in particular the square brackets. You have to protect them from the shell, using double quotes to let them be interpreted literally. Then you have to protect them from grep, which will interpret them as the syntax for character lists. Therefore you have to use double quotes (protection from shell) and backslashes (protection from grep):
Code:
grep ^@08:26:43 testfile | grep -c "\[>>\]"
Note that the >> have to be protected from the shell only, since they have no special meaning in a pattern. At this point choose the method you prefer. An aside note: you don't really need to use the cat command and pipe the output to grep, since grep accept a file name as argument.
 
Old 08-19-2008, 10:47 AM   #10
simpi
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Original Poster
Rep: Reputation: 0
ok, thank you very much. I'm happy it works.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pattern match and counting script -->how to write ? froglinux LQ Suggestions & Feedback 4 06-11-2006 07:10 PM
Three Years and Counting! trickykid General 9 01-06-2004 07:30 PM
And counting... MasterC General 18 09-24-2003 06:07 AM
counting dummyagain Programming 3 09-24-2003 03:28 AM


All times are GMT -5. The time now is 09:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration