Skip to Content
Linux Shell Scripting Cookbook - Third Edition
book

Linux Shell Scripting Cookbook - Third Edition

by Clif Flynt, Sarath Lakshman, Shantanu Tushar
May 2017
Beginner
552 pages
28h 47m
English
Packt Publishing
Content preview from Linux Shell Scripting Cookbook - Third Edition

Special variables

Some special variables that can be used with awk are as follows:

  • NR: This stands for the current record number, which corresponds to the current line number when awk uses lines as records.
  • NF: This stands for the number of fields, and corresponds to the number of fields in the current record being processed. The default field delimiter is a space.
  • $0: This is a variable that contains the text of the current record.
  • $1: This is a variable that holds the text of the first field.
  • $2: This is a variable that holds the text of the second field.

Consider this example:

    $ echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7" | \

    awk '{
        print "Line no:"NR",No of fields:"NF, "$0="$0,   
        "$1="$1,"$2="$2,"$3="$3 
    }' 
 Line no:1,No of fields:3 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Linux Shell Scripting - Second Edition

Mastering Linux Shell Scripting - Second Edition

Mokhtar Ebrahim, Andrew Mallett

Publisher Resources

ISBN: 9781785881985