print every nth line

Some time back I was working with lot of data and wanted to analyze only the every 10 line sometimes and sometimes every 20th line. I had to keep doing these changes in vim or otherwise so finally I wrote a program to do this for me. If you need such a program then here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash -
#===============================================================================
#
#          FILE:  print_nth_line.sh
#
#         USAGE:  ./print_nth_line.sh
#
#   DESCRIPTION:  Print every nth line
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:   (),
#       COMPANY:
#       VERSION:  1.0
#       CREATED:  12/08/2010 05:36:53 PM IST
#      REVISION:  ---
#===============================================================================

if [ x"$1" == "x" ]
then
echo "Please provide the line numbers to print .. .ex 3 will print 3,6,9"
exit -1;
fi
if [ x"$2" == "x" ]
then
echo "Please provide the filename"
exit -2;
fi
awk 'temp++ { if ( temp % '$1' == 0) print $1};' $2
Enhanced by Zemanta

analyze debug queries output for wordpress

Some time back, my website became too slow and I started getting timeout response for quite a lot of my pages. When I analyzed things, I found the issue was with the DB queries taking a lot of time. So, I thought of getting my hands dirty and started with installing the plugin “Debug Queries”. Just in case, you don’t know about the plugin, it lists all the queries to DB along with the time taken for the query when a Admin user visits any page.

The output of the plugin is below the whole page and looks something like this:

45. Time: 0.0030910968780518
Query: SELECT * FROM <>  WHERE <>
Call from: require

Note: The list contains the actual complete query and also all the calls to the query. But as I had more than 40 odd queries, looking at .00 something time was tiresome to find the highest time. So, I copied this text into a text file called “test” and wrote this one line to get me the highest time.

1
sed -n '/.*Time:/ s/.*://p' test |sort -n

Once you have the time, simply search or grep for this values and you know the highest time taking query.

Enhanced by Zemanta