Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| linux:awk [02-05-2017 13:12] – edmc73 | linux:awk [05-03-2018 13:55] (Version actuelle) – edmc73 | ||
|---|---|---|---|
| Ligne 34: | 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 47: | 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 61: | 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 90: | 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 102: | 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 114: | 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 126: | 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 138: | 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 150: | 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 162: | 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. | ||
| + | |||
| + | |||
| + | Réaliser une addtion sur une colonne avec AWK | ||
| + | ps -u www-data -o user,size | sed 1d | awk '{ SUM += $2 } END { print $1" " SUM }' | ||