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 |
Quote:
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. |
You can count matching lines using the -c option of grep, for example
Code:
grep ^@08:26:32 file | grep -c [\>\>] |
OR:
grep -c "^@08:26:32.*>>" filename |
Thank you both guys.
|
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 |
Really strange. Following the last post by pixellany you can refine his pattern using:
Code:
grep "^@08:26:32..... \[>>\]" log |
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 :( |
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 "\[>>\]" |
ok, thank you very much. I'm happy it works.
|
All times are GMT -5. The time now is 12:20 PM. |