{"id":125592,"date":"2025-03-25T07:39:46","date_gmt":"2025-03-25T07:39:46","guid":{"rendered":"\/tutorials\/?p=125592"},"modified":"2025-04-23T08:47:21","modified_gmt":"2025-04-23T08:47:21","slug":"awk-command","status":"publish","type":"post","link":"\/tutorials\/awk-command","title":{"rendered":"The awk command in Linux: Understanding syntax, options and common examples"},"content":{"rendered":"<p><strong>awk<\/strong> is a widely used Linux command for text-processing tasks. You can use this<strong> <\/strong>command directly in the terminal to extract data from a text file, scan for patterns, and perform minor actions like formatting text.<\/p><p>This command is also a scripting language, meaning it can be used to write full-fledged programs. However, this article will focus on what you can do with <strong>awk<\/strong> in the terminal to manipulate text files. We will cover syntax, common use cases, and answer the most common questions.<\/p><p>\n\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-syntax-of-the-awk-command\">Syntax of the awk command<\/h2><p>At its core, the <strong>awk<\/strong> command takes two kinds of input: a text file and a set of instructions. This is reflected in the basic syntax:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk '{ action }' filename.txt<\/pre><ul class=\"wp-block-list\">\n<li><strong>action<\/strong> corresponds to the action you want to take on your text file.<\/li>\n\n\n\n<li><strong>filename<\/strong> is the text file.<\/li>\n<\/ul><p>On the most basic level, the <strong>awk<\/strong> command syntax is very simple. All you need is a text file to interact with and an action to perform.<\/p><h3 class=\"wp-block-heading\" id=\"h-options-and-syntax-variations\">Options and syntax variations<\/h3><p>Your basic <strong>awk<\/strong> command can be further extended by adding options:<\/p><ul class=\"wp-block-list\">\n<li><strong>-F:<\/strong> defines a field separator.<\/li>\n\n\n\n<li><strong>-v:<\/strong> defines variables.<\/li>\n\n\n\n<li><strong>-f:<\/strong> reads the script from a file.<\/li>\n<\/ul><p>Since <strong>awk<\/strong> treats whitespace (spaces or tabs) as the default delimiter between fields in a file or input, <strong>-F<\/strong> tells it how to interpret the columns or fields in each line based on a delimiter. In other words, when you use <strong>-F<\/strong>, <strong>awk<\/strong> knows how to split each line into parts (fields).<br><\/p><p>Using your document from before, you can use <strong>-F<\/strong> as a command line argument to define the colon as the field separator.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -F':' '\/house\/ { print \"ID:\", $1, \"- Type:\", $2, \"- Location:\", $3 }' filename.txt<\/pre><p><strong>awk<\/strong> identifies the separator and interprets the fields accordingly:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ID: 1 - Type: Big house - Location: New York\n\nID: 2 - Type: Small house - Location: Los Angeles\n\nID: 4 - Type: Houseboat - Location: Seattle<\/pre><p>To assign a variable from the command line, you can run:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -v word=\"house\" '$0 ~ word { print $0 }' filename.txt<\/pre><p><strong>word<\/strong> is now a variable that can be used in your action.<\/p><p>Finally, the <strong>-f<\/strong> option is useful for running multiple <strong>awk<\/strong> commands at once from the command line within a single script. Imagine you have a file <strong>simple_script.awk<\/strong> containing the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Print the line number and the line content if the line contains the word \"house\"\n\n$0 ~ \/house\/ { print NR, $0 }\n\n# Print a message before every output\n\nBEGIN { print \"Starting to search for 'house'...\" }<\/pre><p>You can run this with:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -f simple_script.awk filename.txt<\/pre><p>And you&rsquo;ll have:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Starting to search for 'house'...\n1:Big house:New York\n2:Small house:Los Angeles\n4:Houseboat:Seattle<\/pre><h3 class=\"wp-block-heading\" id=\"h-creating-a-sample-file\">Creating a sample file<\/h3><p>Before we discuss use cases, you will need to create a sample file.<\/p><p>For the sake of this example, we will continue to use houses and locations as examples, but create a brand new input file.<\/p><p>To do this, simply use the <strong>touch<\/strong> command to create a new file:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">touch houses.txt<\/pre><p>Since the file is empty, we need to populate it. Let&rsquo;s also change up the houses from our first example: we may want a small house in Vermont, a large house in San Diego, an apartment in New York, and a houseboat in London. We will also add the square meters for each home.<\/p><p>You can use your preferred text editor (e.g., <strong>nano<\/strong> or <strong>vim<\/strong>), or append data directly with <strong>echo<\/strong>.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo -e \"1:Small house:Vermont:100 sqm\\n2:Large house:San Diego:300 sqm\\n3:Apartment:New York:70 sqm\\n4:Houseboat:London:40 sqm\" &gt; houses.txt<\/pre><p>Now, <strong>houses.txt<\/strong> is ready for use in our <strong>awk<\/strong> examples.<\/p><h2 class=\"wp-block-heading\" id=\"h-examples-of-the-awk-command\">Examples of the awk command<\/h2><p>Let&rsquo;s see how we can use the <strong>awk<\/strong> command on our <strong>houses.txt<\/strong> in multiple use cases. Below is a list of common scenarios.<\/p><h3 class=\"wp-block-heading\" id=\"h-1-printing-all-lines-of-a-file\">1. Printing all lines of a file<\/h3><p>To print all the lines from an input file, run the following command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk '{print}' houses.txt<\/pre><p>This will return the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1:Small house:Vermont:100 sqm\n\n2:Large house:San Diego:300 sqm\n\n3:Apartment:New York:70 sqm\n\n4:Houseboat:London:40 sqm<\/pre><h3 class=\"wp-block-heading\" id=\"h-2-printing-a-specific-column\">2. Printing a specific column<\/h3><p>As we already saw, <strong>awk<\/strong> splits each line of a text file into fields (or columns) using whitespace as the separator. In our case, we are using a colon (:). To print specific columns, we need to know the column&rsquo;s position within the line.<br><br>Let&rsquo;s imagine we want to print the column containing the square footage of each home. To do this, we&rsquo;ll run:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -F':' '{print $4}' houses.txt<\/pre><p>The result will be:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">100 sqm\n\n300 sqm\n\n70 sqm\n\n40 sqm<\/pre><p>Here:<\/p><ul class=\"wp-block-list\">\n<li><strong>-F&rsquo;:&rsquo;<\/strong> tells awk to use colon (:) as the field separator.<\/li>\n\n\n\n<li><strong>$4<\/strong> prints the fourth field (square footage).<\/li>\n<\/ul><h3 class=\"wp-block-heading\" id=\"h-3-displaying-lines-that-match-a-pattern\">3. Displaying lines that match a pattern<\/h3><p>Let&rsquo;s imagine you are only interested in the lines of your input file that contain a certain word, or that match a certain pattern. To do this, you&rsquo;ll need to use <a href=\"https:\/\/opensource.com\/article\/19\/11\/how-regular-expressions-awk\" target=\"_blank\" rel=\"noopener\">regex<\/a>.<\/p><p>Regex is a pattern-matching technique, and it can be used to create complex patterns to extract very specific parts of text. Here, we will use a very straightforward regular expression.<\/p><p>For example, if you want to print the entire line containing the word &ldquo;Houseboat&rdquo; from your input file, you&rsquo;ll run:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -F ':' '\/Houseboat\/ {print}' houses.txt<\/pre><p>Which will give you:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">4:Houseboat:London:40 sqm<\/pre><p><strong>\/Houseboat\/<\/strong> is the regex pattern: we are telling the system to look for all matching text for the word &ldquo;Houseboat&rdquo;.<\/p><h3 class=\"wp-block-heading\" id=\"h-4-extracting-and-printing-columns-using-field-manipulation\">4. Extracting and printing columns using field manipulation<\/h3><p>You can also manipulate the fields within your text file and print them in a different order.<\/p><p>Let&rsquo;s say you want to print each line of our text file as a real estate listing. You can do:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -F ':' '{print \"For sale:\", $2, \"in\", $3, \".\", \"Square footage:\", $4}' houses.txt<\/pre><p>Running this command will print:<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69dfebd7058a9\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1460\" height=\"226\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/03\/awk-command-mathematical-operations.jpg\/public\" alt=\"You can use the awk command to manipulate fields and extract and print columns in the order you want.\" class=\"wp-image-125599\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/03\/awk-command-mathematical-operations.jpg\/w=1460,fit=scale-down 1460w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/03\/awk-command-mathematical-operations.jpg\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/03\/awk-command-mathematical-operations.jpg\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/03\/awk-command-mathematical-operations.jpg\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/03\/awk-command-mathematical-operations.jpg\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>The command allows you to rearrange and format the fields however you like. For example, you could swap <strong>$2<\/strong> and <strong>$3<\/strong> to print the location before the house type.<\/p><h3 class=\"wp-block-heading\" id=\"h-5-calculating-mathematical-operations\">5. Calculating mathematical operations<\/h3><p>The <strong>awk<\/strong> command can perform calculations.<\/p><p>Let&rsquo;s add a column to our data containing the price of each property.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -F ':' '{print $0, \": $\", NR * 100000}' houses.txt &gt; priced_houses.txt<\/pre><p>This command creates a <strong>priced_houses.txt<\/strong> with prices for all properties. For simplicity&rsquo;s sake, we will make up prices based on the line number with: <strong>$<\/strong>, <strong>NR * 100000<\/strong>.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1:Small house:Vermont:100 sqm: $100000\n\n2:Large house:San Diego:300 sqm: $200000\n\n3:Apartment:New York:70 sqm: $300000\n\n4:Houseboat:London:40 sqm: $400000<\/pre><p>Now that we have some numbers, we can try out mathematical operations.<\/p><p>To calculate the total cost of the properties, you&rsquo;ll be summing the last column, where the prices are stored (<strong>$5<\/strong>):<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -F ':' '{gsub(\"[$,]\", \"\", $5); sum += $5} END {print \"Total cost:\", sum}' priced_houses.txt<\/pre><p>Which prints:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Total cost: 1000000<\/pre><p>Here:<\/p><ul class=\"wp-block-list\">\n<li><strong>gsub(&ldquo;[$,]&rdquo;, &ldquo;&rdquo;, $5)<\/strong> removes any dollar signs or commas from the price in the fifth field (to allow for proper calculation).<\/li>\n\n\n\n<li><strong>sum += $5<\/strong> adds the price to the running total.<\/li>\n\n\n\n<li><strong>END {print &ldquo;Total cost:&rdquo;, sum}<\/strong> prints the total cost after processing all lines.<\/li>\n<\/ul><h3 class=\"wp-block-heading\" id=\"h-6-processing-data-based-on-conditional-statements\">6. Processing data based on conditional statements<\/h3><p>To calculate only the price of selected properties &ndash; let&rsquo;s say the apartment in New York and the houseboat in London &ndash; you will have to use conditional statements.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -F ':' '($2 == \"Apartment\" || $2 == \"Houseboat\") {gsub(\"[$,]\", \"\", $5); sum += $5} END {print \"NY + LDN, total cost:\", sum}' priced_houses.txt<\/pre><p>In this example:<\/p><ul class=\"wp-block-list\">\n<li><strong>$2 == &ldquo;Apartment&rdquo; || $2 == &ldquo;Houseboat&rdquo;<\/strong> is the condition that ensures that only lines containing &ldquo;Apartment&rdquo; or &ldquo;Houseboat&rdquo; are processed. <strong>||<\/strong> is the conditional symbol for &ldquo;OR&rdquo;.<\/li>\n\n\n\n<li><strong>gsub(&ldquo;[$,]&rdquo;, &ldquo;&rdquo;, $5)<\/strong> removes the dollar signs or commas.<\/li>\n\n\n\n<li><strong>sum += $5<\/strong> adds the price to the sum.<\/li>\n\n\n\n<li><strong>END {print &ldquo;NY + LDN, total cost:&rdquo;, sum}<\/strong> prints the total cost for the selected properties.<\/li>\n<\/ul><p>The above command will print:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">NY + LDN, total cost: 700000<\/pre><p>Using our original input file <strong>houses.txt<\/strong>, you can use another conditional statement to, for example, only print a property if it&rsquo;s larger than 50 sqm:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -F ':' '{if ($4 &gt; 50) print $2, \"in\", $3, \":\", \"OK\"; else print $2, \"in\", $3, \":\", \"too small.\"}' houses.txt<\/pre><p>The above command uses a simple if-else conditional statement separated by a semicolon (;). It will print:<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69dfebd7066cf\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1460\" height=\"217\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/03\/awk-command-conditional-statements.jpg\/public\" alt=\"With the awk command, you can use conditional statements to act on lines that meet certain criteria.\" class=\"wp-image-125600\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/03\/awk-command-conditional-statements.jpg\/w=1460,fit=scale-down 1460w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/03\/awk-command-conditional-statements.jpg\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/03\/awk-command-conditional-statements.jpg\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/03\/awk-command-conditional-statements.jpg\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/03\/awk-command-conditional-statements.jpg\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>You can find all conditional statements in the <a href=\"https:\/\/www.gnu.org\/software\/gawk\/manual\/gawk.html#Index\" target=\"_blank\" rel=\"noopener\">GNU Awk User Manual<\/a>.<\/p><h3 class=\"wp-block-heading\" id=\"h-7-using-built-in-variables\">7. Using built-in variables<\/h3><p><strong>awk<\/strong> has several built-in variables, both numerical and string-based, that are pre-defined in the language.<\/p><p>Here are the most commonly used:<\/p><ul class=\"wp-block-list\">\n<li><strong>NR<\/strong> (Number of Records)<\/li>\n\n\n\n<li><strong>NF<\/strong> (Number of Fields)<\/li>\n\n\n\n<li><strong>FS<\/strong> (Field Separator)<\/li>\n\n\n\n<li><strong>OFS<\/strong> (Output Field Separator)<\/li>\n\n\n\n<li><strong>FILENAME<\/strong><\/li>\n\n\n\n<li><strong>RS<\/strong> (Record Separator)<\/li>\n<\/ul><p>For example, to display the number of fields in each line, you&rsquo;d run:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -F ':' '{print \"Line\", NR, \"has\", NF, \"fields\"}' houses.txt<\/pre><p>Which would output:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Line 1 has 4 fields\n\nLine 2 has 4 fields\n\nLine 3 has 4 fields\n\nLine 4 has 4 fields<\/pre><p>If you want to use <strong>OFS<\/strong> to specify the separator between fields when printing the output, you can run:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk 'BEGIN {OFS=\"XXXX\"} {print $1, $2, $3, $4}' houses.txt<\/pre><p>Which will print:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1:SmallXXXXhouse:Vermont:100XXXXsqmXXXX\n2:LargeXXXXhouse:SanXXXXDiego:300XXXXsqm\n3:Apartment:NewXXXXYork:70XXXXsqmXXXX\n4:Houseboat:London:40XXXXsqmXXXXXXXX<\/pre><h3 class=\"wp-block-heading\" id=\"h-8-using-user-defined-functions\">8. Using user-defined functions<\/h3><p>In <strong>awk<\/strong>, you can manipulate your text more efficiently by using functions directly on the terminal.<\/p><p>For example, to convert the second column (house types) to lowercase, you&rsquo;ll run:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -F ':' '{print tolower($2)}' houses.txt<\/pre><p>Which outputs:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">small house\n\nlarge house\n\napartment\n\nhouseboat<\/pre><p>Here, <strong>tolower($2<\/strong>) is the function being used.<\/p><p>If you want to replace the word &ldquo;house&rdquo; with &ldquo;mansion&rdquo; in the second column:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">awk -F ':' '{gsub(\/house\/, \"mansion\", $2); print $2}' houses.txt<\/pre><p>Where <strong>gsub(\/house\/, &ldquo;mansion&rdquo;, $2)<\/strong> is the function.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Small mansion\n\nLarge mansion\n\nApartment\n\nHouseboat<\/pre><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-vps-hosting\" href=\"\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/public\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Linux&rsquo;s <strong>awk<\/strong> command is a powerful processing tool developers can use to extract, manipulate, and process data from text files. It can be particularly useful for tasks like parsing logs or even CSV files, as it supports mathematical operations, pattern matching, and field manipulation.<\/p><p>By mastering the basics of <strong>awk<\/strong>, you&rsquo;ll soon be able to use it efficiently across tasks, keeping documents streamlined and having powerful functions at your fingertips.<\/p><h2 class=\"wp-block-heading\" id=\"h-awk-command-faq\">awk command FAQ<\/h2><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1742888208035\"><h3 class=\"schema-faq-question\">What is awk best used for?<\/h3> <p class=\"schema-faq-answer\"><strong>&#8203;&#8203;awk<\/strong> is a powerful tool for both arithmetic and string operations. It is best used for text processing, extracting, manipulating structured data, pattern matching, field-based operations, and calculations.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1742888213870\"><h3 class=\"schema-faq-question\">How is awk different from sed?<\/h3> <p class=\"schema-faq-answer\">Both of these are <a href=\"\/tutorials\/linux-commands\">Linux commands<\/a>. However,<strong> sed<\/strong> is best suited for line-based editing and basic text manipulation, while <strong>awk<\/strong> is a complete programming language that allows for conditionals and calculations as well as field-based data processing.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1742888218720\"><h3 class=\"schema-faq-question\">Can awk handle large datasets?<\/h3> <p class=\"schema-faq-answer\">Because <strong>awk<\/strong> operates line by line rather than loading the entire file into memory, it can process large datasets. However, when performing extremely complex operations on very large files, performance may suffer.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>awk is a widely used Linux command for text-processing tasks. You can use this command directly in the terminal to [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/tutorials\/awk-command\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":536,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to use the awk command in Linux","rank_math_description":"Simplify text processing with Linux awk. Master pattern matching, field parsing, and data manipulation for a faster workflow.","rank_math_focus_keyword":"awk command","footnotes":""},"categories":[22648,22644],"tags":[],"class_list":["post-125592","post","type-post","status-publish","format-standard","hentry","category-managing-monitoring-and-security","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/awk-command","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/awk-command","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/awk-command","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/awk-command","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/awk-command","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/awk-command","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/awk-command","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/awk-command","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/125592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/users\/536"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/comments?post=125592"}],"version-history":[{"count":7,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/125592\/revisions"}],"predecessor-version":[{"id":126216,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/125592\/revisions\/126216"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/media?parent=125592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/categories?post=125592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/tags?post=125592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}