IMPORTANT!

Snipt is going open source. We've toyed with this idea for quite a while, and have finally decided it's the right way to move forward.

A few things:
  • The entire Snipt source code will be released on GitHub under the 3-clause BSD License on Friday, September 10th.
  • While we'd like to think we're perfect, we realize we're only human. By open sourcing the software that runs this website, certain bugs or security flaws may be discovered that could compromise the privacy of your snipts.
  • Only the Lion Burger team will be able to push commits to the Snipt.net site. Contributors should send a pull request to add new features or submit patches.
  • By using this site, you agree not to be too angry or take any legal action against Lion Burger should this whole thing go up in flames some day.
  • Follow us on Twitter for updates.
I agree, close this message
Sign up to create your own snipts, or login.

Latest 100 public snipts » awk The latest public awk snipts.

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
    	}
        '
    

    copy | embed

    0 comments - tagged in  posted by d1s4st3r on Jun 17, 2010 at 9:09 a.m. EDT
  • Check apache memory consumption
    ps aux | egrep ^apache | awk 'BEGIN {x=0} {x+=$4} END {print x}'
    

    copy | embed

    0 comments - tagged in  posted by malkir on May 06, 2010 at 9:25 p.m. EDT
  • Match curly brackets
    awk '/({[^}]*$)|(^[^{]*})/{print}' myfile
    

    copy | embed

    0 comments - tagged in  posted by d1s4st3r on May 04, 2010 at 4:42 a.m. EDT
  • 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
    

    copy | embed

    0 comments - tagged in  posted by benyounes on Apr 30, 2010 at 3:40 p.m. EDT
  • 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
    

    copy | embed

    0 comments - tagged in  posted by d1s4st3r on Apr 29, 2010 at 3:40 a.m. EDT
  • 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
    

    copy | embed

    0 comments - tagged in  posted by cgv on Apr 12, 2010 at 6:22 a.m. EDT
  • 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 }
    '
    

    copy | embed

    0 comments - tagged in  posted by d1s4st3r on Apr 07, 2010 at 5:34 a.m. EDT
  • number of file open by command
    lsof | awk '{printf("%s\n",$1)}' | sort | uniq -c | sort -n
    

    copy | embed

    0 comments - tagged in  posted by flavin on Mar 11, 2010 at 1:57 p.m. EST
  • 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
    

    copy | embed

    0 comments - tagged in  posted by cgv on Jan 28, 2010 at 8:52 a.m. EST
  • provides list of matching files out of "grep -h"
    awk '{gsub(":",""); print $1}' | sort -u
    

    copy | embed

    0 comments - tagged in  posted by jbachman01 on Jan 22, 2010 at 11:34 a.m. EST
  • 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
    

    copy | embed

    0 comments - tagged in  posted by cgv on Jan 05, 2010 at 4:52 a.m. EST
  • 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 
    

    copy | embed

    0 comments - tagged in  posted by rschu68 on Dec 02, 2009 at 9:50 a.m. EST
  • Get column names in MySQL
    mysql -u <user> --password=<password> -e "SHOW COLUMNS FROM <table>" <database> | awk '{print $1}' | tr "\n" "," | sed 's/,$//g'
    

    copy | embed

    0 comments - tagged in  posted by juque on Nov 06, 2009 at 1:36 a.m. EST
  • Simple command line calculator
    calc(){ awk "BEGIN{ print $* }" ;}
    
    # some usage examples:
    # calc 3+3
    # calc "((3+(2^3)) * 34^2 / 9)-75.89"
    

    copy | embed

    0 comments - tagged in  posted by d1s4st3r on Nov 03, 2009 at 5:40 p.m. EST
  • 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
    

    copy | embed

    0 comments - tagged in  posted by tayhimself on Oct 22, 2009 at 3:49 p.m. EDT
  • find duplicate lines in a file
    sort [file] | uniq -c | awk '$1 !~/1/'
    

    copy | embed

    0 comments - tagged in  posted by tayhimself on Sep 28, 2009 at 12:20 p.m. EDT
  • 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
    

    copy | embed

    0 comments - tagged in  posted by AvidDeville on Sep 04, 2009 at 3:34 p.m. EDT
  • 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
    

    copy | embed

    0 comments - tagged in  posted by fraktil on Aug 08, 2009 at 6:50 p.m. EDT
  • print the list of local IP addresses from ifconfig
    #/bin/sh
    ifconfig | grep 'inet addr' | awk '{print $2}' | cut -f 2 -d ':' 
    

    copy | embed

    0 comments - tagged in  posted by fegabe on Jul 23, 2009 at 8:36 a.m. EDT
  • 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]); }'
    

    copy | embed

    0 comments - tagged in  posted by d1s4st3r on Jun 14, 2009 at 6:23 a.m. EDT
Sign up to create your own snipts, or login.