Annotated edit history of
AWKNotes version 4, including all changes.
View license author blame.
| Rev |
Author |
# |
Line |
| 1 |
BenStaz |
1 |
!The Field Separator |
| |
|
2 |
|
| |
|
3 |
It doesn't just have to white space between fields. In can be anything as long as you set the appropriate field separator. |
| |
|
4 |
Eg: |
| |
|
5 |
*echo "123:456:789" | awk -F ':' '{print $1,$2,$3,}' |
| |
|
6 |
|
| |
|
7 |
The '':'' after the ''-F'' tells us that '':'' is the input field separator. Change this as appropriate. |
| |
|
8 |
|
| |
|
9 |
!Multiple Field Separators. |
| |
|
10 |
|
| |
|
11 |
What if there are multiple characters you wish to use as field separators? |
| |
|
12 |
After the ''-F'' just list all of the characters in square brackets. |
| |
|
13 |
For example: |
| |
|
14 |
|
| |
|
15 |
*echo "123:456=789" | awk -F '~[:=]' '{print $1,$2,$3}' |
| 2 |
BenStaz |
16 |
|
| |
|
17 |
!Using Bash Variables inside awk. |
| |
|
18 |
|
| |
|
19 |
To use the value of a bash variable. You have to use the ''-v'' switch to import it. |
| |
|
20 |
The syntax is: |
| |
|
21 |
|
| |
|
22 |
*awk -v awkvar=$bashvar {print awkvar} |
| |
|
23 |
|
| |
|
24 |
Now you can use awkvar in your awk statements which will have the value of bashvar. |
| 3 |
IanMcDonald |
25 |
Remember you *CANNOT* change the value of a bash variable using [AWK]. |
| 4 |
AlastairPorter |
26 |
|
| |
|
27 |
! Floating point output |
| |
|
28 |
you can use printf modifiers to specify the output of numbers, for example |
| |
|
29 |
<verbatim> |
| |
|
30 |
printf "%.02f",a/b |
| |
|
31 |
</verbatim> |