November 24th, 2010 Leave a comment Go to comments

awk tutorial:

awk string manipulation:


Get last file/dir changed:

latest_file=$(ls -lrt | tail -1 | awk '{print $NF}')

To show the second column in a text file:

awk '{print $2}' < filename.txt

To show the second row in a text file containing specified text. The text file is feeded from stdout through a pipe:

cat fil.txt|awk '/^visstext/ {print $2}'

Show “ok” if a ping succeeds (tested on a Q700 NAS):

ping -c 1 -q|awk '{if ($7=="0%") printf "ok\n"}'

To clean up a file you can use grep to remove all comments and sed to remove all empty lines, thanks to David Klein for this tip:

grep -v "^#" /etc/squid/squid.conf | sed -e '/^$/d'

Rename all files by replacing the first character with a “x”:

for o in *;do n=`echo "$o"|awk '{print "x" substr($0,2)}'`;mv "$o" "$n";done
  1. No comments yet.
  1. No trackbacks yet.