Latest 100 public
snipts » awk
showing 1-20 of 31 snipts for awk
-
∞ Script that determines the MIME type of a file
#!/bin/bash ########################################################################## # Title : getmimetype - determine MIME type of file # Author : Heiner Steven <heiner.steven@odn.de> # Date : 1999-08-24 # Requires : [ngm]awk # Category : Mail # SCCS-Id. : @(#) getmimetype 1.7 06/07/19 ########################################################################## PN=`basename "$0"` # Program name VER='1.7' : ${LOCALLIB:=/usr/local/lib} # We need a new version of AWK, i.e. "nawk" or "gawk" awk= for path in `echo "$PATH" | sed 's/^:/.:/;s/:$/:./;s/:/ /g'` do for p in mawk gawk nawk do [ -x "$path/$p" ] || continue awk=$path/$p; break 2 done done : ${NAWK:=${awk:=awk}} Usage () { echo >&2 "$PN - determine MIME type of file, $VER usage: $PN file [file ...] Reads the files $LOCALLIB/mimetypes $HOME/.mimetypes" exit 1 } Msg () { for MsgLine do echo "$PN: $MsgLine" >&2 done } Fatal () { Msg "$@"; exit 1; } set -- `getopt h "$@"` [ $# -lt 1 ] && Usage # "getopt" detected an error while [ $# -gt 0 ] do case "$1" in --) shift; break;; -h) Usage;; -*) Usage;; *) break;; # First file name esac shift done MimeTypes=${TMPDIR:=/tmp}/pmt$$ trap 'rm -f "$MimeTypes" >/dev/null 2>&1' 0 trap "exit 2" 1 2 3 15 > "$MimeTypes" for file in $LOCALLIB/mimetypes $HOME/.mimetypes do if [ -r "$file" ] then cat "$file" >> "$MimeTypes" fi done # Define some MIME type defaults cat <<-EOT >> "$MimeTypes" # MIME type File Extensions text/plain .*\.[tT][eE][xX][tT]$ .*\.[tT][xX][tT]$ text/html .*\.[hH][tT][mM]$ .*\.[hH][tT][mM][lL]$ image/jpeg .*\.[jJ][pP][eE][gG]$ .*\.[jJ][pP][gG]$ image/gif .*\.[gG][iI][fF]$ image/png .*\.[pP][nN][gG]$ video/mpeg .*\.[mM][pP][eE][gG]$ .*\.[mM][pP][gG]$ video/avi .*\.[aA][vV][iI]$ application/postscript .*\.[pP][sS]$ application/pdf .*\.[pP][dD][fF]$ audio/basic .*\.[aA][uU]$ audio/wav .*\.[wW][aA][vV]$ audio/mp3 .*\.[mM][pP]3$ EOT for file do [ -f "$file" ] || continue if file "$file" | grep -i text >/dev/null then defaulttype=text/plain else defaulttype=application/octet-stream fi echo "$defaulttype $file" done | $NAWK ' BEGIN { # Read the MIME type table mimetypes = "'"$MimeTypes"'" while ( getline < mimetypes ) { if ( $1 ~ /^\#/ ) continue # ignore comments if ( $0 ~ /^[ ]*$/ ) continue # ignore empty lines if ( (k = split ($0, f)) > 1 ) { mimetype = f [1] for ( i=2; i<=k; i++ ) typeOf [$i] = mimetype } } close (mimetypes) #for ( i in typeOf ) print "<" i ">", typeOf [i] } { mimetype = "" for ( t in typeOf ) { if ( $0 ~ t ) { mimetype = typeOf [t] break } } if ( mimetype == "" ) mimetype = $1 print mimetype } '
-
∞ Check apache memory consumption
ps aux | egrep ^apache | awk 'BEGIN {x=0} {x+=$4} END {print x}' -
∞ Match curly brackets
awk '/({[^}]*$)|(^[^{]*})/{print}' myfile -
∞ Query shoutcast for internet radios by keyword
keyword="70";query="http://www.shoutcast.com/directory/searchKeyword.jsp?numresult=25&maxbitrate=All&s=";declare -a playlink ; playlink=($(curl -s $query$keyword|awk -v FS="'" '/tuneIn/ {++PL[$2]} END {for(a in PL) print "http://yp.shoutcast.com/sbin/tunein-station.pls?id="a}'));curl -s $query$keyword|awk -F '="' '/target/ {++ST[$3]} END {for(a in ST) print "> "a}'|awk -v FS=">" ' /href/ {print ++i,$0}';i=1;for item in ${playlink[*]} ; do echo $i" >" $item;((i++));done
-
∞ Show the number of processes owned by each user on the system
ps aux | awk '{$1} {++P[$1]} END {for(a in P) if (a != "USER") print a, P[a]}' | column -t -
∞ create multiple screen session for a specific function
#!/bin/bash # # it starts multiple screen session whose usual task is remote ssh over # a line separated list of IP's # session_name : the prefix of the name of the screen sessions # script_name : implements the remote ssh code e.g. with python's paramiko # input_file_prefix : the prefix of the files for the script to process # iterations : the number of input files which is equivalent to the screen sessions to be spawned (indexing is zero-based) # if [ $# != 4 ] ; then printf "usage: %s: session_name script_name input_file_prefix iterations\n" $0 exit 1 fi SESSION=$1 SCRIPT=$2 INPUT=$3 NUMBER=$4 awk -v name=$SESSION -v script=$SCRIPT -v file=$INPUT -v times=$NUMBER ' BEGIN{ if( times <= 0 ) exit 2 for( i = 0; i < times; i++ ) { strCommand = sprintf( "screen -S %s%02d -md ./%s %s%02d", name, i, script, file, i ); printf strCommand "\n" system( strCommand ); } }' exit 0
-
∞ A very small GAWK script that splits a MySQL text dump to separate table dump files
#!/bin/bash # GPLv2 by Erno Rigo <mcree_AT_tricon_DOT_hu> gawk ' BEGIN { FS=" "; dest="/dev/null" } /^(USE)/ { dir=gensub(";","","g",$2) ; dir=gensub("`","","g",dir) ; system("mkdir "dir) } /^(CREATE|USE|DROP)/ { fflush(dest); dest="/dev/null" } /^CREATE TABLE/ { file=gensub("`","","g",$3) ; fflush(dest); dest=dir"/"file".sql" } { print >> dest } '
-
∞ 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]); }'


