w3reference home
AWK Tutorial


Bookmark and Share

AWK Variables

awk variables are initialized to either zero or the empty string the first time they are used.
Variables
  • Variable declaration is not required
  • May contain any type of data, their data type may change over the life of the program
  • Must begin with a letter and continuing with letters, digits and underscores
  • Are case senstive
  • Some of the commonly used built-in variables are:
    • NR -- The current line's sequential number
    • NF -- The number of fields in the current line
    • FS -- The input field separator; defaults to whitespace and is reset by the -F command line parameter
    abhishek@ubuntu-a:~/Desktop/test$ cat calc
    3 56
    567 89
    awkuser@ubuntu-a:~/Desktop/test$ awk '{d=($2-($1-4));s=($2+$1);print d/sqrt(s),d*d/s }' calc
    7.42077 55.0678
    -18.5066 342.494
    awkuser@ubuntu-a:~/Desktop/test$ 
    
    in above example we have a file calc with two rows and two columns. Note that the final statement, a "print" in this case, does not need a semicolon. It doesn't hurt to put it in, though.

    Integer variables can be used to refer to fields. If one field contains information about which other field is important, this script will print only the important field:
    $ awk '{imp=$1; print $imp }' calc
    

    The special variable NF tells you how many fields are in this record. This script prints the first and last field from each record, regardless of how many fields there are:
    if now calc file is
    3 56 abd
    567 89 xyz
    
    $ awk '{print $1,$NF }' calc
    3 abd
    567 xyz
    
  • 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.