Shell & Grep: Where we count certain strings in a log file by hour.
for var in `seq 10 23`;
do
echo 'Feb 28 '$var;
grep 'Feb 28 '$var /log/cli/Hanson/2015-02-28.log | grep 'Duplicate order cancel' | wc -l;
done
#!/bin/bash
seq 0 9 | awk "{ print \"convert cake_layers.png -crop 300x154+0+\" \$0*154 \" cl_\" \$0 \".png\" }" | bash
$ ls myfile
$ ^myfile^myotherfile
find . -type f -exec grep -i "lookfor" {} \; -print
ssh -f user@somebox " . ./.bashrc; cd /apps/myapp/;
cat ../pid | xargs kill;
nohup ./startMyApp 1>../stdout 2>../stderr & echo $! > ../pid
ssh -f user@somebox " . ./.bashrc; cd /apps/myapp/; cat ../pid | xargs kill; sleep 10; nohup ./startMyApp 1>../stdout 2>../stderr & echo $! > ../pid' > startMyAppRemote.log
After going slowly crazy trying to remember how an if statement is formed in shell with its lovely and entirely useless help messages.
I've decided to post this:
#!/bin/sh
FULLHOSTNAME=`hostname -f`
if [ "$FULLHOSTNAME" = "loneqflocfd0025.uk.db.com" ]; then
echo "hi"
fi
Notes:
You need a space after '[' and before ']'
You need a ';'
...and while I'm here - here is how to do a regex if:
if [[ "los" =~ lo.* ]]; then
...String matching with variables
When comparing strings " is very different to '
' will not evaluate variables
" will evaluate them
Hence:
fish=haddock
echo 'Hello $fish'=== Hello $fish
echo "Hello $fish" === Hello haddock
Using dates and comparing current time with something that may have taken less than 2 seconds:
start=`date +%s`
# Do something
now=`date +%s`
if [ $start -gt $((now - 2)) ];
then
printf "Fast fail detected. I know about:\n"
fi
while read host; do
echo "running on $host"
ssh -n $host "ls " >> output.txt;
done < boxes.txt
for loop in {281..284}
do
echo "running on frread@nygeqgd0$loop.us.db.com"
ssh -n frread@nygeqgd0$loop.us.db.com "ls " >> output.txt;
done
for loop in `seq 281 284`
#!/bin/sh
LOOK_FOR="codehaus/xfire/spring"
for i in `find . -name "*jar"`
do
echo "Looking in $i ..."
jar tvf $i | grep $LOOK_FOR > /dev/null
if [ $? == 0 ]
then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
done
awk '{ split($1, ar, "="); sum += ar[2]; } END { print sum; }' < ab2.txt
col1=`echo $s1 | awk '{print $1}'` col2=`echo $s1 | awk '{print $2}'`
ls | awk '{ split($1, ar, "_"); print ar[1] }' | uniq
Awk for http logs. Find all 400 errors in this todays log:
head -100 2014-02-13.log | awk -v FS='\t' '{ print $6,$0 }' | grep ^40*
Alternate way of finding 500 errors:
cat /log/nginx_access/current | awk '$9 == "500" { print $0 }' | less
start=$(date --date '5 apr 2014 0:00' +%s) stop=$(date --date '5 apr 2014 23:00' +%s) #These are expressed in seconds. You can use a for loop on their values, increasing them by 1 hour (3600 seconds): for t in $(seq ${start} 3600 ${stop}) do d=$(date --date @${t} +'%d/%b/%Y:%H') d_pretty=$(date --date @${t} +'%d-%b-%Y-%H') #echo $d grep $d 2014-04-05.log | cut -f 8 | sort | uniq -c| sort -rn | head -20 > out_$d_pretty.txt done
grep -c RuntimeError 2015-10-*