Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision Les deux révisions suivantes | |||
linux:awk [02-05-2017 15:09] edmc73 créée |
linux:awk [02-05-2017 15:12] edmc73 |
||
---|---|---|---|
Ligne 23: | Ligne 23: | ||
< | < | ||
- | %i or d --Decimal%o --Octal | + | %i or d --Decimal |
+ | %o --Octal | ||
%x --hex | %x --hex | ||
%c --ASCII number character | %c --ASCII number character | ||
Ligne 33: | Ligne 34: | ||
Lets start with some examples. for this post our test file contents are | Lets start with some examples. for this post our test file contents are | ||
- | < | + | |
- | Jones 21 78 84 77 | + | '' |
Gondrol 23 56 58 45 | Gondrol 23 56 58 45 | ||
RinRao 25 21 38 37 | RinRao 25 21 38 37 | ||
Edwin 25 87 97 95 | Edwin 25 87 97 95 | ||
- | Dayan 24 55 30 47 | + | Dayan 24 55 30 47'' |
- | </ | + | |
Example 1: Print first column values from db.txt file. | Example 1: Print first column values from db.txt file. | ||
Ligne 46: | Ligne 47: | ||
Output: | Output: | ||
- | < | + | |
- | Jones | + | '' |
Gondrol | Gondrol | ||
RinRao | RinRao | ||
Edwin | Edwin | ||
- | Dayan | + | Dayan'' |
- | </ | + | |
Note: printf will not have default new line char, so you have to include tat when ever you execute printf command as shown above. | Note: printf will not have default new line char, so you have to include tat when ever you execute printf command as shown above. | ||
Ligne 60: | Ligne 61: | ||
Output: | Output: | ||
- | < | + | |
+ | '' | ||
0 | 0 | ||
0 | 0 | ||
0 | 0 | ||
- | 0 | + | 0'' |
- | 0 | + | |
- | </ | + | |
So be careful when dealing with different data types. | So be careful when dealing with different data types. | ||
Padding between columns using AWK printf | Padding between columns using AWK printf | ||
Ligne 89: | Ligne 90: | ||
Output: | Output: | ||
- | < | + | |
- | 217884 | + | '' |
235658 | 235658 | ||
252138 | 252138 | ||
258797 | 258797 | ||
- | 245530 | + | 245530'' |
- | </ | + | |
With padding 5 spaces on right hand side: | With padding 5 spaces on right hand side: | ||
Ligne 101: | Ligne 102: | ||
Output: | Output: | ||
- | < | + | |
- | 21 | + | '' |
23 | 23 | ||
25 | 25 | ||
25 | 25 | ||
- | 24 | + | 24 |
- | </ | + | |
Note: As for understanding purpose we given this example with padding and without padding. We will add “|” between columnts output so that that padding can be clearly seen as show in below example. | Note: As for understanding purpose we given this example with padding and without padding. We will add “|” between columnts output so that that padding can be clearly seen as show in below example. | ||
Ligne 113: | Ligne 114: | ||
Output: | Output: | ||
- | < | + | |
- | |21 | + | '' |
|23 | |23 | ||
|25 | |25 | ||
|25 | |25 | ||
- | |24 | + | |24 |
- | </ | + | |
Example 5: Pad 5 spaces on left hand side of each column. | Example 5: Pad 5 spaces on left hand side of each column. | ||
Ligne 125: | Ligne 126: | ||
Output: | Output: | ||
- | < | + | |
- | | | + | '' |
| | | | ||
| | | | ||
| | | | ||
- | | | + | | |
- | </ | + | |
Example 6: add zero’s on left hand side of each column element make it a 5 digit number. | Example 6: add zero’s on left hand side of each column element make it a 5 digit number. | ||
Ligne 137: | Ligne 138: | ||
Output: | Output: | ||
- | < | + | |
- | |00021|00078|00084| | + | '' |
|00023|00056|00058| | |00023|00056|00058| | ||
|00025|00021|00038| | |00025|00021|00038| | ||
|00025|00087|00097| | |00025|00087|00097| | ||
- | |00024|00055|00030| | + | |00024|00055|00030|'' |
- | </ | + | |
Example 7: Make the column element with 4 digit’s and 7 in length and print the number to left hand side. | Example 7: Make the column element with 4 digit’s and 7 in length and print the number to left hand side. | ||
Ligne 149: | Ligne 150: | ||
Output: | Output: | ||
- | < | + | |
- | |0021 | + | '' |
|0023 | |0023 | ||
|0025 | |0025 | ||
|0025 | |0025 | ||
- | |0024 | + | |0024 |
- | </ | + | |
Example 8: Make the column element with 4 digit’s and 7 in length and print the number to right hand side. | Example 8: Make the column element with 4 digit’s and 7 in length and print the number to right hand side. | ||
Ligne 161: | Ligne 162: | ||
Output: | Output: | ||
- | < | + | |
- | | | + | '' |
| | | | ||
| | | | ||
| | | | ||
- | | | + | | |
- | </ | + | |
Hope these examples help you to understand about formatting output and padding using printf statement in AWK scripting. Keep visiting www.linuxnix.com for more scripting tutorials and tips. | Hope these examples help you to understand about formatting output and padding using printf statement in AWK scripting. Keep visiting www.linuxnix.com for more scripting tutorials and tips. |