Public
snipts » awk
showing 1-20 of 24 snipts for awk
-
∞ number of file open by command
lsof | awk '{printf("%s\n",$1)}' | sort | uniq -c | sort -n -
∞ elapsed time between two log records
#!/bin/bash # # parses the time (in seconds) that elapsed between 2 consecutive entries # each matching to pattern1 and pattern2. # date format : [DDD] [MMM] [dd] [hh]:[mm]:[ss] [YYYY] # example : Tue May 12 06:49:44 2009 # # substitute the patterns at will # # limitation : it works for entries having the same year # # example # Tue May 12 06:49:41 2009 - pattern1 # Tue May 12 06:49:44 2009 - pattern2 # # it should output : # 3 # awk ' BEGIN{ m["Jan"]="01"; m["Feb"]="02"; m["Mar"]="03"; m["Apr"]="04"; m["May"]="05"; m["Jun"]="06"; m["Jul"]="07"; m["Aug"]="08"; m["Sep"]="09"; m["Oct"]="10"; m["Nov"]="11"; m["Dec"]="12"; d["Sun"]="01"; d["Mon"]="02"; d["Tue"]="03"; d["Wed"]="04"; d["Thu"]="05"; d["Fri"]="06"; d["Sat"]="07"; s=0; # start timestamp e=0; # end timestamp } /pattern1/ { dt1=$0; gsub(":"," ",$4); spec1=sprintf("%s %s %s %s",$5,d[$1],m[$2],$4); s=mktime(spec1); } /pattern2/ { dt2=$0; gsub(":"," ",$4); spec2=sprintf("%s %s %s %s",$5,d[$1],m[$2],$4); e=mktime(spec2); print e-s; e=s=0; }' < $1
-
∞ provides list of matching files out of "grep -h"
awk '{gsub(":",""); print $1}' | sort -u -
∞ sort filezilla server log entries by session id
#!/bin/bash # # entry example # # (1579051) 10/16/2009 0:02:13 AM - (not logged in) (192.168.1.11)> USER chris # # sort -k1n fzs-2009-10-16.log # does not work properly because when the session id and timestamp are the same # it goes on and tries to sort based on the message # if the timestamp granularity was high enough it would work (and so much faster!) # [ $# -ne 1 ] && echo "number of args required is 1" && exit 1 [ ! -e $1 ] && echo "file $1 does not exist" && exit 2 FILEPATH=`dirname $1` LOGNAME=`basename $1` LOGNAME_SORTED=${FILEPATH}/${LOGNAME}".sorted" SID_SORTED_FILENAME=${FILEPATH}/"fzlog_sid_sorted.$$" rm -f $LOGNAME_SORTED awk '{ split( $1, L, "\\(|\\)" ); print L[ 2 ]; }' $1 | sort | uniq > ${SID_SORTED_FILENAME} cat ${SID_SORTED_FILENAME} | while read line do grep "^($line)" $1 >> ${LOGNAME_SORTED} done rm -f ${SID_SORTED_FILENAME} exit 0
-
∞ avoid piping grep to awk
# Avoid Piping Grep to Awk http://hacktux.com/bash/script/efficient If using Awk, you can often eliminate the need for grep. Try not to pipe Grep to Awk: # avoid this grep error /var/log/messages | awk '{ print $4 }' Use Awk's native ability to parse text and save yourself a command. awk '/error/ { print $4 }' /var/log/messages -
∞ Get column names in MySQL
mysql -u <user> --password=<password> -e "SHOW COLUMNS FROM <table>" <database> | awk '{print $1}' | tr "\n" "," | sed 's/,$//g'
-
∞ Simple command line calculator
calc(){ awk "BEGIN{ print $* }" ;} # some usage examples: # calc 3+3 # calc "((3+(2^3)) * 34^2 / 9)-75.89"
-
∞ Sed search replace
#replace 999 with nothing sed 's/999//g' output.csv > output-no999.csv #awk print a particular column awk '{print $1"\t"$2}' file.csv
-
∞ find duplicate lines in a file
sort [file] | uniq -c | awk '$1 !~/1/'
-
∞ awk find access_log
awk '$1~/THINGYOUWANT/' /var/www/vhosts/*/statistics/logs/access_log | head #or for all occurrences use below, head only prints top 10 lines awk '$1~/THINGYOUWANT/' /var/www/vhosts/*/statistics/logs/access_log
-
∞ Update /etc/fstab with currently mounted devices
mv /etc/fstab /etc/fstab.old && mount|awk '{print $1, $3, $5, $6}'|sed s/\(//g|sed s/\)/' 0 0'/g >> /etc/fstab -
∞ print the list of local IP addresses from ifconfig
#/bin/sh ifconfig | grep 'inet addr' | awk '{print $2}' | cut -f 2 -d ':'
-
∞ List every file and subdirectory in the current directory sorted by size
du -sk ./* | sort -n | awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x > 1024 ) { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total: %g%s\n",int(total*10)/10,pref[y]); }' -
∞ awk command for printing out a list of unique IP addresses from an apache access_log
awk '{ip[$1]=0} END {for (address in ip) { print address} }' /home/admin/logs/access_log -
∞ bash variable in awk
awk '{print "'"$VAR1"'", "'"$VAR2"'"}'
-
∞ Get the IP address of a local ethernet interface
ifconfig <INTERFACE> | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1 }'
-
∞ Show most frequently used commands
history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -n | tail
-
∞ Show an autoupdating clock in the console
#!/bin/bash while true do clear banner "Exact time" " `date | awk '{ print $4 }'`" "`date | awk '{ print $3$2" "$6 }'`" sleep 1 done
-
∞ find long lines
awk '{if (length>80) print NR, $0}' FILE -
∞ Learning IP in OSX
ifconfig en0 | awk '/inet / { print $2 } '



CSS: The Missing Manual