awk

awk tutorial: http://www.grymoire.com/Unix/Awk.html#uh-0

awk string manipulation: http://tldp.org/LDP/abs/html/string-manipulation.html

Examples

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 192.168.0.1|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. Inga kommentarer än.
  1. Inga trackbacks än.