Sign up to create your own snipts, or login.

Public snipts » awk The latest public awk snipts.

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
    

    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
  • 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
    

    copy | embed

    0 comments - tagged in  posted by pkarl on May 30, 2009 at 10:02 p.m. EDT
  • bash variable in awk
    awk '{print "'"$VAR1"'", "'"$VAR2"'"}'
    

    copy | embed

    0 comments - tagged in  posted by benash on May 12, 2009 at 10:00 p.m. EDT
  • Get the IP address of a local ethernet interface
    ifconfig <INTERFACE> | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1 }'
    

    copy | embed

    0 comments - tagged in  posted by d1s4st3r on Apr 29, 2009 at 3:08 a.m. EDT
  • Show most frequently used commands
    history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -n | tail
    

    copy | embed

    0 comments - tagged in  posted by d1s4st3r on Apr 15, 2009 at 5:11 a.m. EDT
  • 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
    

    copy | embed

    0 comments - tagged in  posted by d1s4st3r on Apr 12, 2009 at 11:53 a.m. EDT
  • find long lines
    awk '{if (length>80) print NR, $0}' FILE
    

    copy | embed

    0 comments - tagged in  posted by inky on Feb 11, 2009 at 8:51 p.m. EST
  • Learning IP in OSX
    ifconfig en0 | awk '/inet / { print $2 } '
    

    copy | embed

    0 comments - tagged in  posted by isagoksu on Jan 15, 2009 at 1:46 p.m. EST
Sign up to create your own snipts, or login.