Linux Shell Scripting Tutorial (LSST) v1.05r3 | ||
Chapter 7: awk Revisited | ||
|
You can even take input from keyboard while running awk script, try the following awk script:
|
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:
|
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
|
Run it as follows:
$ awk -f awkread_file1
Try to understand the following awk script and note down its output.
temp2final1.awk
Real life examples in awk | sed - Quick Introduction |