|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>awk Command Explanation</title> |
| 7 | + <link rel="stylesheet" href="style.css"> |
| 8 | +</head> |
| 9 | +<body> |
| 10 | + <div class="container"> |
| 11 | + <h1>Understanding the <code>awk</code> Command</h1> |
| 12 | + |
| 13 | + <section> |
| 14 | + <h2>Basic Usage of <code>awk</code></h2> |
| 15 | + <p>The <code>awk</code> command is a powerful tool for pattern scanning and processing in text files. It processes each line in a file, splitting them into fields based on a delimiter and performing actions on these fields.</p> |
| 16 | + <div class="code-block"> |
| 17 | + <code>awk 'pattern { action }' [file...]</code> |
| 18 | + </div> |
| 19 | + <p>Where <code>pattern</code> is the condition for processing a line and <code>action</code> is the operation performed on the matched lines.</p> |
| 20 | + </section> |
| 21 | + |
| 22 | + <section> |
| 23 | + <h2>Options Available with <code>awk</code></h2> |
| 24 | + |
| 25 | + <h3><code>awk '{print}' [file]</code> (Basic Output)</h3> |
| 26 | + <p>Prints each line of the file:</p> |
| 27 | + <div class="code-block"> |
| 28 | + <code>awk '{print}' file.txt</code> |
| 29 | + </div> |
| 30 | + <p>This command prints all lines from <code>file.txt</code> without any filtering.</p> |
| 31 | + |
| 32 | + <h3><code>awk '{print $1}' [file]</code> (Print Specific Fields)</h3> |
| 33 | + <p>Prints the first field from each line:</p> |
| 34 | + <div class="code-block"> |
| 35 | + <code>awk '{print $1}' file.txt</code> |
| 36 | + </div> |
| 37 | + <p>This will print only the first word or field from each line in the file.</p> |
| 38 | + |
| 39 | + <h3><code>awk -F ":" '{print $1}' [file]</code> (Custom Field Separator)</h3> |
| 40 | + <p>Use a custom field separator (in this case, a colon <code>:</code>):</p> |
| 41 | + <div class="code-block"> |
| 42 | + <code>awk -F ":" '{print $1}' file.txt</code> |
| 43 | + </div> |
| 44 | + <p>This prints the first field from each line using the colon as a delimiter, common in files like <code>/etc/passwd</code>.</p> |
| 45 | + |
| 46 | + <h3><code>awk '/pattern/ {print}' [file]</code> (Pattern Matching)</h3> |
| 47 | + <p>Search for a pattern in the file and print matching lines:</p> |
| 48 | + <div class="code-block"> |
| 49 | + <code>awk '/example/ {print}' file.txt</code> |
| 50 | + </div> |
| 51 | + <p>This prints only the lines that contain the word "example".</p> |
| 52 | + |
| 53 | + <h3><code>awk '{print $1, $3}' [file]</code> (Print Multiple Fields)</h3> |
| 54 | + <p>Print multiple fields from each line:</p> |
| 55 | + <div class="code-block"> |
| 56 | + <code>awk '{print $1, $3}' file.txt</code> |
| 57 | + </div> |
| 58 | + <p>This prints both the first and third fields from each line.</p> |
| 59 | + |
| 60 | + <h3><code>awk 'NR==3' [file]</code> (Print Specific Line)</h3> |
| 61 | + <p>Print only the third line from the file:</p> |
| 62 | + <div class="code-block"> |
| 63 | + <code>awk 'NR==3' file.txt</code> |
| 64 | + </div> |
| 65 | + <p>This command outputs the third line of the file.</p> |
| 66 | + |
| 67 | + <h3><code>awk 'NR==2, NR==4' [file]</code> (Print a Range of Lines)</h3> |
| 68 | + <p>Print a range of lines (e.g., from line 2 to line 4):</p> |
| 69 | + <div class="code-block"> |
| 70 | + <code>awk 'NR==2, NR==4' file.txt</code> |
| 71 | + </div> |
| 72 | + <p>This command prints lines 2, 3, and 4 from the file.</p> |
| 73 | + |
| 74 | + <h3><code>awk '{sum += $1} END {print sum}' [file]</code> (Sum a Field)</h3> |
| 75 | + <p>Sum the values of a specific field:</p> |
| 76 | + <div class="code-block"> |
| 77 | + <code>awk '{sum += $1} END {print sum}' file.txt</code> |
| 78 | + </div> |
| 79 | + <p>This command adds all the values in the first field and prints the total.</p> |
| 80 | + |
| 81 | + <h3><code>awk '{if ($1 > 10) print}' [file]</code> (Conditional Statements)</h3> |
| 82 | + <p>Print lines where the first field is greater than 10:</p> |
| 83 | + <div class="code-block"> |
| 84 | + <code>awk '{if ($1 > 10) print}' file.txt</code> |
| 85 | + </div> |
| 86 | + <p>This command only prints lines where the first field has a value greater than 10.</p> |
| 87 | + |
| 88 | + <h3><code>awk 'BEGIN {print "Header"} {print $1} END {print "Footer"}' [file]</code> (BEGIN and END Blocks)</h3> |
| 89 | + <p>Print a header before processing and a footer after processing:</p> |
| 90 | + <div class="code-block"> |
| 91 | + <code>awk 'BEGIN {print "Header"} {print $1} END {print "Footer"}' file.txt</code> |
| 92 | + </div> |
| 93 | + <p>This prints "Header" first, then the first field from each line, followed by "Footer" at the end.</p> |
| 94 | + |
| 95 | + <h3><code>awk --help</code></h3> |
| 96 | + <p>Displays help information for the <code>awk</code> command:</p> |
| 97 | + <div class="code-block"> |
| 98 | + <code>awk --help</code> |
| 99 | + </div> |
| 100 | + </section> |
| 101 | + |
| 102 | + <section> |
| 103 | + <h2>Summary of Options</h2> |
| 104 | + <table> |
| 105 | + <thead> |
| 106 | + <tr> |
| 107 | + <th>Option</th> |
| 108 | + <th>Description</th> |
| 109 | + </tr> |
| 110 | + </thead> |
| 111 | + <tbody> |
| 112 | + <tr> |
| 113 | + <td><code>awk '{print}' [file]</code></td> |
| 114 | + <td>Prints each line from the file.</td> |
| 115 | + </tr> |
| 116 | + <tr> |
| 117 | + <td><code>awk '{print $1}' [file]</code></td> |
| 118 | + <td>Prints the first field of each line.</td> |
| 119 | + </tr> |
| 120 | + <tr> |
| 121 | + <td><code>awk -F ":" '{print $1}' [file]</code></td> |
| 122 | + <td>Use a colon as the field delimiter.</td> |
| 123 | + </tr> |
| 124 | + <tr> |
| 125 | + <td><code>awk '/pattern/ {print}' [file]</code></td> |
| 126 | + <td>Print lines that match a given pattern.</td> |
| 127 | + </tr> |
| 128 | + <tr> |
| 129 | + <td><code>awk '{print $1, $3}' [file]</code></td> |
| 130 | + <td>Print multiple fields.</td> |
| 131 | + </tr> |
| 132 | + <tr> |
| 133 | + <td><code>awk 'NR==3' [file]</code></td> |
| 134 | + <td>Print only the third line from the file.</td> |
| 135 | + </tr> |
| 136 | + <tr> |
| 137 | + <td><code>awk 'NR==2, NR==4' [file]</code></td> |
| 138 | + <td>Print a range of lines (from line 2 to 4).</td> |
| 139 | + </tr> |
| 140 | + <tr> |
| 141 | + <td><code>awk '{sum += $1} END {print sum}' [file]</code></td> |
| 142 | + <td>Sum the values of a field.</td> |
| 143 | + </tr> |
| 144 | + <tr> |
| 145 | + <td><code>awk '{if ($1 > 10) print}' [file]</code></td> |
| 146 | + <td>Print lines where the first field is greater than 10.</td> |
| 147 | + </tr> |
| 148 | + <tr> |
| 149 | + <td><code>awk 'BEGIN {print "Header"} {print $1} END {print "Footer"}' [file]</code></td> |
| 150 | + <td>Print a header, process lines, and print a footer.</td> |
| 151 | + </tr> |
| 152 | + <tr> |
| 153 | + <td><code>awk --help</code></td> |
| 154 | + <td>Display help information for <code>awk</code>.</td> |
| 155 | + </tr> |
| 156 | + </tbody> |
| 157 | + </table> |
| 158 | + </section> |
| 159 | + |
| 160 | + <section> |
| 161 | + <h2>Manual Pages</h2> |
| 162 | + <p>For more detailed information, use the manual page for the <code>awk</code> command:</p> |
| 163 | + <div class="code-block"> |
| 164 | + <code>man awk</code> |
| 165 | + </div> |
| 166 | + </section> |
| 167 | + </div> |
| 168 | +</body> |
| 169 | +</html> |
0 commit comments