w3reference home
AWK Tutorial


Bookmark and Share

AWK Examples

As a second example, suppose you have several thousand files you want to move into a new directory and rename by appending a .dat to the filenames. You could do this one by one (several hours), or use vi to make a decent command file to do it (several minutes), or use awk (several seconds).

Suppose the files are named junk* (* is wildcard for any sequence of characters), and need to be moved to ../iraf and have a '.dat' appended to the name. To do this type
ls junk* | awk '{print "mv "$0" ../iraf/"$0".dat"}' | csh

Print first two fields in opposite order:
  awk '{ print $2, $1 }' file

Print lines longer than 45 characters
  awk 'length > 45' file

Print length of string in 4th column
  awk '{print length($4)}' file

Add up first column, print sum and average
       
{ s += $1 }
END  { print "sum is", s, " average is", s/NR }

Print fields in reverse order
awk '{ for (i = NF; i > 0; --i) print $i }' file

Print the last line
      {line = $0}
           END {print line}

Print the total number of lines that contain the word Mike
  /Mike/ {nlines = nlines + 1}
  END {print nlines}

Print all lines between start/stop pairs
  awk '/start/, /stop/' file

Print all lines whose first field is different from previous one
  awk '$1 != prev { print; prev = $1 }' file

Print column 3 if column 1 > column 2
  awk '$1 > $2 {print $3}' file

Print line if column 3 > column 2
  awk '$3 > $2' file

Count number of lines where col 3 > col 1
  
awk '$3 > $1 {print i + "1"; i++}' file

Print sequence number and then column 1 of file
  awk '{print NR, $1}' file

Print every line after erasing the 2nd field
  awk '{$2 = ""; print}' file

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

Print hi.0010 to hi.0099
  
yes | head -90 | awk '{printf("hi00%2.0f n", NR+9)}'

Print out 4 random numbers between 0 and 1
yes | head -4 | awk '{print rand()}'

Print out 40 random integers modulo 5
yes | head -40 | awk '{print int(100*rand()) % 5}'
Replace every field by its absolute value
  
{ for (i = 1; i <= NF; i=i+1) if ($i < 0) $i = -$i print}

If you have another character that delimits fields, use the -F option

For example, to print out the phone number for Jones in the following file

# 000902|Smith|Theodore|333-242-2222|149092
# 000901|Jones|Bill|532-382-0342|234023
# ...
# type
  awk -F"|" '$2=="Jones"{print $4}' filename

Some looping commands
Remove a bunch of print jobs from the queue
  
BEGIN{
	for (i=175;i>133;i--){
	printf "lprm -Plw %dn", i
	} exit
     }
Code Validator
Learn FTP
Color finder
Link Checker
Free web designs
Coming soon!
Interview Questions...
'w3reference : Learn by examples ... Advanced to beginner's tutorials ...'
Ajax: AJAX tutorial1 | Apache: Apache HTTP Server | Restarting Apache | AWK: awk Select | awk Control Statements | awk Patterns | awk Variables | CSS: CSS Border | CSS Syntax | CSS Selector | CSS Comment | CVS: CVS Release | CVS Login | CVS Logout | CVS Annotate | Databases: Rolap Tutorial | OLAP Tutorial | OLTP Tutorial | data warehousing | Expect: HTML: html | Linux: Dot (.) conf files | Linux Mount Point | Linux Filesystem | SSH Tutorial | Linux Commands: cal | cat | cfdisk | chroot | MySQL: MySQL Commands | PHP: PHP Basics | PHP Variables | PHP Output (echo/print) | PHP String Concat | PL/SQL: PL/SQL Data Types | PL/SQL Control Structures | PL/SQL File Extensions | PL/SQL DBMS_OUTPUT package | Python: My first Python program | SED: Shell: Starting Bash | Bash Redirection | Bash Pipes | Bash Variables | SQL: SQL Transactions | SQL Constraints | SQL Drop | SQL Union & Union All | SVN: svn architecture | SVN Repository | SVN Import | SVN Checkout | Tech: soap | Web Designing: Web Hosting | HTML/XHTML/CSS code validator | Learn FTP | Search Engine Optimization Tips | www: XML: XML vs HTML | XML Syntax | XML Tags, Elements and Attributes | XML Namespaces |
Sitemap | Disclaim | Privacy Policy | Contact | ©2007-2009 w3reference.com All Rights Reserved.