0

My abc.log contains below entries (snippet):

...
INFO #my-service# #add# id=67986324423 isTrial=true
INFO #my-service# #add# id=43536343643 isTrial=false
INFO #my-service# #add# id=43634636365 isTrial=true
INFO #my-service# #add# id=67986324423 isTrial=true
INFO #my-service# #delete# id=43634636365 isTrial=true
INFO #my-service# #delete# id=56543435355 isTrial=false
...

I want to count the lines which are having unique ids with #add# attribute in them & having isTrial=true.

For above snippet, the output should be 2

Can any one proivide me linux command which I can run against above log file?

4 Answers 4

4

Using just awk:

# Count unique line
$ awk '$3~"add"&&$5~"true"&&!u[$4]++{++c}END{print c}' file
2

# Print unique lines
$ awk '$3~"add"&&$5~"true"&&!u[$4]++' file
INFO #my-service# #add# id=67986324423 isTrial=true
INFO #my-service# #add# id=43634636365 isTrial=true

Or just sort and grep

$ sort -uk4,4 file | grep "#add#.*true" 
INFO #my-service# #add# id=67986324423 isTrial=true
INFO #my-service# #add# id=43634636365 isTrial=true

$ sort -uk4,4 file | grep -c "#add#.*true"
2
6
  • 1
    Fewer processes … I like it +1
    – knittl
    Jan 10, 2013 at 20:21
  • I think this could be cleaner and easier to grok (e.g. awk '$3 == "#add#" && $5 == "isTrial=true" { ++c; } END { print c; }' or awk '$3 == "#add#" && $5 == "isTrial=true" { seen[$4] = 1; } END { for (i in seen) print i; }, respectively - may or may not be more efficient, though), but +1 for the right idea...
    – twalberg
    Jan 10, 2013 at 20:31
  • @twalberg the first script you suggest doesn't account for unique id, I take you point on comparing fields over line matching however. Jan 10, 2013 at 20:36
  • @sudo_O Doh... right... My thinko there - so the first one should actually be as you had it for the action bits : awk '$3 == "#add#" && $5 == "isTrial=true" { seen[$4] = 1; } END { print length(seen); }. Now if only I could edit comments...
    – twalberg
    Jan 10, 2013 at 20:45
  • Which one will run fast...using just awk or using grep cut sort uniq? Jan 11, 2013 at 3:28
1

this one-liner gives you result 2

awk -F'#add# id=' '$2~/true/{a[$2]}END{print length(a)}' abc.log

this one-liner gives you the two unique lines

awk -F'#add# id=' '$2~/true/&&!a[$2]++' abc.log
0

Combine grep, cut, sort and wc:

grep '#add#.*isTrial=true$' /path/to/file | cut -f4 | sort -u | wc -l

Customize the regular expression to your liking (being more strict/less strict about what lines it matches).

(cut -f4 -d' ' if your delimiter is space instead of tab)

3
  • 2
    Wait : cut -f4 does nothing there ! Jan 10, 2013 at 20:07
  • I'd appreciate if the downvoter gave an explanation why he thinks this answer is so bad or incorrect?
    – knittl
    Jan 10, 2013 at 20:29
  • I didn't downvote, but I'm curious: won't that .* be greedy?
    – 1252748
    Jan 31, 2014 at 16:34
-1

this will count too

grep "isTrial=true" abc.log | grep "#add#" | awk ' { print $4 }' | sort | uniq | wc -l 
1
  • thanks...if the id contains alphabets, will the above command still work? Jan 10, 2013 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.