3

I have some log files. And I have a list of user ids. My log files are in a particular format

log-yyyy-mm-dd
e.g. log-2014-09-30

I am trying to search each userId in the log file. This is what I am doing -

for userid in 234, 283, 893, 982, 323; do cat log-2014-09-30 | grep -a "user deleted "$userid  | tail -1; done

Ideally it should search each user id in the file and print the last match. I will then search in log-2014-09-29 for the remaining user ids and so on.

But it is only printing the match for the last user id.

INFO - 2014-09-30 22:49:00 - user deleted 323

What am I doing wrong? please help me out.

5

Get rid of the commas in the userid list. The way to pass a list to a for loop is to give it space separated values, not comma separated:

for userid in 234, 283, 893, 982, 323; do echo "$userid";done

prints

234,
283,
893,
982,
323

But

for userid in 234 283 893 982 323; do echo "$userid";done

prints

234
283
893
982
323

So, you should do

for userid in 234 283 893 982 323; do 
    grep -a "user deleted $userid" log-2014-09-30  | tail -n 1
done
  • Oh yes. My mistake! It works perfect now. Thanks a lot – Hussain Tamboli Oct 22 '14 at 11:43
  • You're welcome, we posted essentially the same answer within seconds of each other so I thought I'd just combine them into yours and delete mine :). Welcome to the site! – terdon Oct 22 '14 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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