Linux/awk

From Omnia
(Redirected from Awk)
Jump to navigation Jump to search

awk

Print all columns from pipe:

... | awk '{print $0}'

Print columns 1 and 2 separated by spaces:

... | awk '{print $1, $2}'
... | awk '{print $1 " " $2}'

Grep for something

... | awk '/somestring/'
... | awk '/somestring/{print $1}'

Sub string:

ESX=$( vmware -v | awk '{print $2 substr($3,1,1) substr($3,3,1)}' )  # ESXi50

For loop with if:

echo "one two three four five" | awk '{for(i=1;i<10;i++) {if($i!="") printf $i " "}}'

Padding with spaces:

awk '{printf "%-30s%s\n", $1, $2}'

Print all remaining fields:

# find longest running processes
ps aux | awk '{ $1=$2=$3=$4=$5=$6=$7=$8=$9=""; print $0 }' | sort -n

Uppercase Lowercase

echo "Hello World" | awk '{print toupper($0)}'
echo "Hello World" | awk '{print tolower($0)}'

powerful awk

"awk is a little programming language, with a syntax close to C in many aspects. It is an interpreted language and the awk interpreter processes the instructions.

Awk is, roughly speaking, a language oriented to manage tables. That is some information which can be grouped inside fields and records. The advantage here is that the record definition (and the field definition) is flexible.

Awk is powerful. It's designed for work with one-line records, but that point could be relaxed"

extract the first column from a file

awk '{print $1}' file

renaming files (append .new to "files_list"):

ls files_list | awk '{print "mv "$1" "$1".new"}' | sh

Renaming within the name: (although in some cases it will fail, as in file_old_and_old)

ls -1 *old* | awk '{print "mv "$1" "$1}' | sed s/old/new/2 | sh

remove only files:

ls -l * | grep -v drwx | awk '{print "rm "$9}' | sh
# or with awk alone:
ls -l|awk '$1!~/^drwx/{print $9}'|xargs rm 

remove only directories

ls -l | grep '^d' | awk '{print "rm -r "$9}' | sh
# or
ls -p | grep /$ | wk '{print "rm -r "$1}'
# or with awk alone:
ls -l|awk '$1~/^d.*x/{print $9}'|xargs rm -r 

killing processes by name (in this example we kill the process called netscape):

kill `ps auxww | grep netscape | egrep -v grep | awk '{print $2}'`
# or with awk alone:
ps auxww | awk '$0~/netscape/&&$0!~/awk/{print $2}' |xargs kill 

Source: UNIX Basics : Examples with awk: A short introduction

Print first two fields in opposite order:

awk '{ print $2, $1 }' file

Print lines longer than 72 characters:

awk 'length > 72' file

Print length of string in 2nd column

awk '{print length($2)}' file

Print hi 28 times

yes | head -28 | awk '{ print "hi" }'
yes hi | head -n 28


Source: Hartigan/Computer/AWK

References:

Famous Awk One-Liners

famous Awk one-liners (.txt)




AWK


String Manipulation

Chopping off the last field of each line? :: Free Tech Support from Ask Dave Taylor!:

rev inputfile | cut -f2- | rev > outputfile
awk '{$NF=""; print $0}'

keywords