Outils pour utilisateurs

Outils du site


linux:awk (lu 30557 fois)

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision Les deux révisions suivantes
linux:awk [02-05-2017 15:12]
edmc73
linux:awk [02-05-2017 15:16]
edmc73 ancienne révision (02-05-2017 15:09) restaurée
Ligne 23: Ligne 23:
  
 <code> <code>
-%i or d --Decimal +%i or d --Decimal%o --Octal
-%o --Octal+
 %x --hex %x --hex
 %c --ASCII number character %c --ASCII number character
Ligne 34: Ligne 33:
  
 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
- +<code> 
-''Jones 21 78 84 77+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 
 +</code>
  
 Example 1: Print first column values from db.txt file. Example 1: Print first column values from db.txt file.
Ligne 47: Ligne 46:
  
 Output: Output:
- +<code> 
-''Jones+Jones
 Gondrol Gondrol
 RinRao RinRao
 Edwin Edwin
-Dayan'' +Dayan 
 +</code>
 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 60:
  
 Output: Output:
- +<code>
-''0+
 0 0
 0 0
 0 0
-0'' +
 +
 +</code>
 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 89:
  
 Output: Output:
- +<code> 
-''217884+217884
 235658 235658
 252138 252138
 258797 258797
-245530'' +245530 
 +</code>
 With padding 5 spaces on right hand side: With padding 5 spaces on right hand side:
  
Ligne 102: Ligne 101:
  
 Output: Output:
- +<code> 
-''21   78   84+21   78   84
 23   56   58 23   56   58
 25   21   38 25   21   38
 25   87   97 25   87   97
-24   55   30'' +24   55   30 
 +</code>
 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 113:
  
 Output: Output:
- +<code> 
-''|21   |78   |84   |+|21   |78   |84   |
 |23   |56   |58   | |23   |56   |58   |
 |25   |21   |38   | |25   |21   |38   |
 |25   |87   |97   | |25   |87   |97   |
-|24   |55   |30   |'' +|24   |55   |30   | 
 +</code>
 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 125:
  
 Output: Output:
- +<code> 
-''|   21|   78|   84|+|   21|   78|   84|
 |   23|   56|   58| |   23|   56|   58|
 |   25|   21|   38| |   25|   21|   38|
 |   25|   87|   97| |   25|   87|   97|
-|   24|   55|   30|'' +|   24|   55|   30| 
 +</code>
 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 137:
  
 Output: Output:
- +<code> 
-''|00021|00078|00084|+|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| 
 +</code>
 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 149:
  
 Output: Output:
- +<code> 
-''|0021   |0078   |0084   |+|0021   |0078   |0084   |
 |0023   |0056   |0058   | |0023   |0056   |0058   |
 |0025   |0021   |0038   | |0025   |0021   |0038   |
 |0025   |0087   |0097   | |0025   |0087   |0097   |
-|0024   |0055   |0030   |'' +|0024   |0055   |0030   | 
 +</code>
 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 161:
  
 Output: Output:
- +<code> 
-''|   0021|   0078|   0084|+|   0021|   0078|   0084|
 |   0023|   0056|   0058| |   0023|   0056|   0058|
 |   0025|   0021|   0038| |   0025|   0021|   0038|
 |   0025|   0087|   0097| |   0025|   0087|   0097|
-|   0024|   0055|   0030|'' +|   0024|   0055|   0030| 
 +</code>
 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.
linux/awk.txt · Dernière modification: 05-03-2018 14:55 de edmc73