Linux Shell Scripting Tutorial (LSST) v1.05r3
Prev
Chapter 7: awk Revisited
Next

awk miscellaneous

You can even take input from keyboard while running awk script, try the following awk script:

$ cat > testusrip
BEGIN {
      printf "Your name please:"
      getline na < "-"
      printf "%s your age please:",na
      getline age < "-"
     print "Hello " na, ", next year you will be " age + 1
}

Save it and run as
$ awk -f testusrip
Your name please: Vivek
Vivek your age please: 26
Hello Vivek, next year you will be 27

Here getline function is used to read input from keyboard and then assign the data (inputted from keyboard) to variable.
Syntax:
getline variable-name < "-"
|              |                      |
1            2                     3

1 --> getline is function name
2 --> variable-name is used to assign the value read from input
3 --> Means read from stdin (keyboard)

To reading Input from file use following
Syntax:
getline < "file-name"

Example:
getline < "friends.db"

To reading Input from pipe use following
Syntax:
"command" | getline

Example:

$ cat > awkread_file
BEGIN {
     "date" | getline
     print $0

}


Run it as
$ awk -f awkread_file
Fri Apr 12 00:05:45 IST 2002

Command date is executed and its piped to getline which assign the date command output to variable $0. If you want your own variable then replace the above program as follows

$ cat > awkread_file1
BEGIN {
     "date" | getline today
     print today

}

Run it as follows:
$ awk -f awkread_file1

Try to understand the following awk script and note down its output.
temp2final1.awk


Prev
Home
Next
Real life examples in awk
Up
sed - Quick Introduction