View Single Post
  #6   (View Single Post)  
Old 24th August 2010
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,318
Default

Quote:
Originally Posted by guitarscn View Post
How are your personal experiences with sed/awk?
When it comes to shell programming, sed(1) & awk(1) are indispensible.

For example, here's code I use within a shell script configuring a USB drive as a install medium (so I don't have to continually burn CD's...). Note that I have numerous subshells exploiting sed(1) & awk(1):
Code:
# ensure checksums match                                                                                      
verify_checksums()
{
    typeset FILE LOCAL SERVER ERROR=0

    for FILE in * ; do
        if [ $FILE != SHA256 ] ; then
            echo $FILE | awk '{ printf "%-20s", $1 }'
            SERVER=$(egrep "^SHA256 \($FILE\)" SHA256 | awk '{ print $4 }')
            LOCAL=$(cksum -a sha256 $FILE | sed 's!^.* = \(.*\)$!\1!')
            if [ $LOCAL = $SERVER ] ; then
                echo 'OK'
            else
                echo 'not OK'
                ERROR=1
            fi
        fi
    done
        
    [ $ERROR -ne 0 ] && panic 'SHA256 mismatch encountered'
}
This is fairly typical of the code I write. Perhaps it might be possible to write it with other utilities, but sed(1) & awk(1) are both powerful & malleable, so I continue to use them.
Reply With Quote