LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   counting script (https://www.linuxquestions.org/questions/linux-newbie-8/counting-script-663809/)

simpi 08-19-2008 07:25 AM

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 :confused:

thank you

pixellany 08-19-2008 07:38 AM

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.

colucix 08-19-2008 07:44 AM

You can count matching lines using the -c option of grep, for example
Code:

grep ^@08:26:32 file | grep -c [\>\>]

pixellany 08-19-2008 07:52 AM

OR:
grep -c "^@08:26:32.*>>" filename

simpi 08-19-2008 08:35 AM

Thank you both guys.

simpi 08-19-2008 09:03 AM

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

colucix 08-19-2008 09:10 AM

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.

simpi 08-19-2008 09:22 AM

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 :(

colucix 08-19-2008 09:33 AM

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.

simpi 08-19-2008 09:47 AM

ok, thank you very much. I'm happy it works.


All times are GMT -5. The time now is 12:20 PM.