Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The mysql general log file looks like this...

110822 14:41:14 103700 Query    SHOW FULL TABLES FROM `engine` LIKE 'PROBABLYNOT'
                103700 Query    SHOW FULL TABLES FROM `engine` LIKE 'PROBABLYNOT'
110822 15:01:14 103701 Query    SHOW FULL TABLES FROM `dbname` LIKE 'PROBABLYNOT'

The thread ID 103701 can be found at the beginning or at number 3 position (after date-time) I am using the following command to find all the queries executed in a particular thread.

grep -w '103701' /var/log/mysql/general.log | cut -c 1-1000 | tail -10

This shows a lot of lines where 103701 number is mentioned. I am looking for this number only at first or third position. What should the correct grep statement for this?

share|improve this question
up vote 2 down vote accepted

You can use awk to do this:

$ awk '$1 == "103701" || $3 == "103701" { print $0 }' mysql-general.log
share|improve this answer
When I use it shell script, something like ... # awk '$1 == "$thread_id" The variable name does not get replaced. – shantanuo Aug 24 '11 at 10:33
Wrap it with a single quote: $1 == "'$thread_id'". – quanta Aug 24 '11 at 10:42
extraordinary answer! – shantanuo Aug 24 '11 at 10:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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