<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amal Shehu</title>
    <description>The latest articles on DEV Community by Amal Shehu (@amalshehu).</description>
    <link>https://dev.to/amalshehu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F15032%2F8bb0d69d-448f-49f8-a6b8-2bc63f5c07a2.png</url>
      <title>DEV Community: Amal Shehu</title>
      <link>https://dev.to/amalshehu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amalshehu"/>
    <language>en</language>
    <item>
      <title>Advanced Data Processing with awk and sed</title>
      <dc:creator>Amal Shehu</dc:creator>
      <pubDate>Wed, 10 Jan 2024 21:47:28 +0000</pubDate>
      <link>https://dev.to/amalshehu/advanced-data-processing-with-awk-and-sed-4f2b</link>
      <guid>https://dev.to/amalshehu/advanced-data-processing-with-awk-and-sed-4f2b</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F59t5jpgyk2zpqfc2fqj1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F59t5jpgyk2zpqfc2fqj1.png" alt="Programmer"&gt;&lt;/a&gt;&lt;br&gt;
Processing large log files is a common yet challenging task in data management. The Unix tools &lt;code&gt;awk&lt;/code&gt; and &lt;code&gt;sed&lt;/code&gt; are indispensable for such tasks, providing powerful capabilities for pattern scanning, data extraction, and stream editing.&lt;/p&gt;
&lt;h4&gt;
  
  
  Introduction to awk and sed
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;awk:&lt;/strong&gt; A powerful programming language designed for text processing. It's particularly suited for processing structured text data, like tables, and excels in pattern scanning and processing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed:&lt;/strong&gt; Short for Stream Editor, &lt;code&gt;sed&lt;/code&gt; is a utility that performs basic text transformations on an input stream (a file or input from a pipeline). It's primarily used for substituting text patterns.&lt;/p&gt;
&lt;h4&gt;
  
  
  Understanding the Tools
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;awk Syntax:&lt;/strong&gt; &lt;code&gt;awk 'pattern {action}' file&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pattern&lt;/code&gt;: Specifies the condition to be matched.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;action&lt;/code&gt;: The set of commands to execute when the pattern matches.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;file&lt;/code&gt;: The file to be processed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;sed Syntax:&lt;/strong&gt; &lt;code&gt;sed [options] 'command' file&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;options&lt;/code&gt;: Modifiers that alter the behavior of &lt;code&gt;sed&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;command&lt;/code&gt;: The sed command to execute (like substitution).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;file&lt;/code&gt;: The file to process.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;awk&lt;/code&gt; and &lt;code&gt;sed&lt;/code&gt; are not just tools; they are powerful allies in text processing and data manipulation. Their integration into Unix/Linux environments and scripting capabilities make them indispensable for anyone working with text and data. Beginner or an experienced professional, mastering these tools can significantly enhance productivity and broaden our capabilities in data handling.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example 1: Extracting and Formatting Data from a Log File
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; From a log file, &lt;code&gt;server.log&lt;/code&gt;, extract records from a specific date, format them into CSV, and perform some clean-up tasks. Each line follows this format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Date] [Time] [IP Address] [URL]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our goal is to extract records from a specific date, format them into CSV, and perform some clean-up tasks.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1: Inspecting the Log File
&lt;/h4&gt;

&lt;p&gt;Sample &lt;code&gt;server.log&lt;/code&gt; entries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2024-01-10 09:15:23 192.168.1.1 /index.html
2024-01-10 09:17:45 192.168.1.2 /about.html
2024-01-11 10:15:24 192.168.1.3 /contact.html
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 2: Filtering and Formatting with awk
&lt;/h4&gt;

&lt;p&gt;To extract and format records from &lt;code&gt;2024-01-10&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'$1 == "2024-01-10" {print $1","$2","$3","$4}'&lt;/span&gt; server.log &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; output.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$1 == "2024-01-10"&lt;/code&gt;: Filters lines where the first field (date) is &lt;code&gt;2024-01-10&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;print $1","$2","$3","$4&lt;/code&gt;: Formats the selected line into CSV format and writes to &lt;code&gt;output.csv&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 3: Advanced Text Manipulation with sed
&lt;/h4&gt;

&lt;p&gt;Suppose we want to clean the URLs by removing &lt;code&gt;http://www.example.com&lt;/code&gt; and changing &lt;code&gt;/index.html&lt;/code&gt; to &lt;code&gt;/home.html&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/http:\/\/www.example.com//g'&lt;/span&gt; output.csv
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/\/index.html/\/home.html/g'&lt;/span&gt; output.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first &lt;code&gt;sed&lt;/code&gt; command removes the specified URL part.&lt;/li&gt;
&lt;li&gt;The second &lt;code&gt;sed&lt;/code&gt; command replaces &lt;code&gt;/index.html&lt;/code&gt; with &lt;code&gt;/home.html&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Final Output
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;output.csv&lt;/code&gt; will now contain clean, well-formatted data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2024-01-10,09:15:23,192.168.1.1,/home.html
2024-01-10,09:17:45,192.168.1.2,/about.html
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 2: Extracting Specific Fields and Summarizing Data
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; From a log file, &lt;code&gt;sales.log&lt;/code&gt;, extract sales data for a particular product, summarize total sales.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Log File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Date, Product ID, Quantity, Price
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;, &lt;span class="s1"&gt;'$2 == "Product123" {total += $3 * $4} END {print "Total Sales for Product123: ", total}'&lt;/span&gt; sales.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-F,&lt;/code&gt;: Sets field separator as comma.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$2 == "Product123"&lt;/code&gt;: Filters records for 'Product123'.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{total += $3 * $4}&lt;/code&gt;: Calculates total sales.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;END {print ...}&lt;/code&gt;: Prints the final total.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 3: Combining awk and sed for Data Cleanup and Transformation
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; From &lt;code&gt;server.log&lt;/code&gt;, extract data, reformat dates, and remove specific query parameters from URLs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server Log Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;YYYY-MM-DD HH:MM:SS IP /path?query
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Commands:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'$1 &amp;gt;= "2024-01-01" &amp;amp;&amp;amp; $1 &amp;lt;= "2024-01-31" {print $1","$2","$3","$4}'&lt;/span&gt; server.log | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-E&lt;/span&gt; &lt;span class="s1"&gt;'s/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3-\2-\1/'&lt;/span&gt; | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s/\?.*//'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;awk&lt;/code&gt; filters and formats January 2024 data.&lt;/li&gt;
&lt;li&gt;First &lt;code&gt;sed&lt;/code&gt; changes date format to &lt;code&gt;DD-MM-YYYY&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Second &lt;code&gt;sed&lt;/code&gt; removes query parameters.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Example 4: Analyzing and Reformatting Multi-Line Records
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Process a multi-line log file where each record spans multiple lines, and reformat the output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Multi-Line Log Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start: [Date]
Info: [Details]
End: [Date]
---
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'/Start/ {start = $2} /Info/ {info = $2} /End/ {print "Record from " start " to " $2 ": " info}'&lt;/span&gt; multi-line.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses pattern matching over multiple lines.&lt;/li&gt;
&lt;li&gt;Accumulates data and prints a summary for each record.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 5: Sed for Advanced In-Place Editing
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Modify &lt;code&gt;config.txt&lt;/code&gt; by replacing specific configuration values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Commands:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'/max_connections/c\max_connections=500'&lt;/span&gt; config.txt
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'/timeout/s/30/60/'&lt;/span&gt; config.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Changes &lt;code&gt;max_connections&lt;/code&gt; to 500.&lt;/li&gt;
&lt;li&gt;Replaces timeout value 30 with 60.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Example 6: awk for Column-Based Arithmetic Operations
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Calculate average memory usage from a system log file, &lt;code&gt;system.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;System Log Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Timestamp, CPU Load, Memory Usage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;, &lt;span class="s1"&gt;'{total += $3; count++} END {print "Average Memory Usage:", total/count}'&lt;/span&gt; system.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Summarizes memory usage and calculates average.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 7: Filtering and Counting Unique IP Addresses
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; From a web server log file, &lt;code&gt;access.log&lt;/code&gt;, extract and count unique IP addresses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Log File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IP Address - Date Time Request ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $1}'&lt;/span&gt; access.log | &lt;span class="nb"&gt;sort&lt;/span&gt; | &lt;span class="nb"&gt;uniq&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;awk '{print $1}'&lt;/code&gt;: Extracts the first field, typically the IP address.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sort&lt;/code&gt;: Sorts the IP addresses.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;uniq -c&lt;/code&gt;: Counts the occurrences of each unique IP address.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 8: Processing Multi-Field Records and Summarizing
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; In a file &lt;code&gt;inventory.csv&lt;/code&gt;, calculate total value of each product category.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSV File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Category,Product,Quantity,Unit Price
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;, &lt;span class="s1"&gt;'{total[$1] += $3 * $4} END {for (cat in total) print cat, total[cat]}'&lt;/span&gt; inventory.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-F,&lt;/code&gt;: Sets field separator as comma.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{total[$1] += $3 * $4}&lt;/code&gt;: Aggregates total value per category.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;END {for (cat in total) ...}&lt;/code&gt;: Iterates over categories to print total values.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 9: sed for Line Deletion Based on Pattern
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Remove all comments from a configuration file, &lt;code&gt;config.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configuration File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# This is a comment
parameter=value
# Another comment
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'/^#/d'&lt;/span&gt; config.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/^#/d&lt;/code&gt;: Deletes lines that start with &lt;code&gt;#&lt;/code&gt; (comments).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 10: awk for Complex Pattern Matching and Extraction
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; From a log file &lt;code&gt;security.log&lt;/code&gt;, extract timestamps of failed login attempts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Log File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Timestamp] [Log Level] [Message]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'/Failed login/ {print $1}'&lt;/span&gt; security.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/Failed login/&lt;/code&gt;: Filters lines containing 'Failed login'.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{print $1}&lt;/code&gt;: Prints the timestamp field of those lines.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 11: Extracting and Sorting Log Entries by Date
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Sort &lt;code&gt;application.log&lt;/code&gt; entries by date in descending order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Log File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[YYYY-MM-DD] [Message]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk and sort Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $1, $2}'&lt;/span&gt; application.log | &lt;span class="nb"&gt;sort&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;awk '{print $1, $2}'&lt;/code&gt;: Extracts the date and time.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sort -r&lt;/code&gt;: Sorts the results in reverse (descending) order.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 12: Merging Two Files Line by Line
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Merge lines from &lt;code&gt;file1.txt&lt;/code&gt; and &lt;code&gt;file2.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'NR==FNR{a[NR]=$0; next} {print a[FNR], $0}'&lt;/span&gt; file1.txt file2.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;NR==FNR&lt;/code&gt;: True for the first file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;a[NR]=$0; next&lt;/code&gt;: Stores the lines of the first file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{print a[FNR], $0}&lt;/code&gt;: Prints the corresponding lines from both files.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 13: Replacing Multiple Patterns with sed
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; In &lt;code&gt;config.txt&lt;/code&gt;, replace multiple outdated parameters with new ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s1"&gt;'s/oldparam1/newparam1/'&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s1"&gt;'s/oldparam2/newparam2/'&lt;/span&gt; config.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple &lt;code&gt;-e&lt;/code&gt; options allow for applying several substitution patterns.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 14: awk for Column-Wise Summation
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Sum values in the third column of &lt;code&gt;data.csv&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSV File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Field1,Field2,Value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;, &lt;span class="s1"&gt;'{sum += $3} END {print sum}'&lt;/span&gt; data.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-F,&lt;/code&gt;: Sets the comma as the field separator.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{sum += $3}&lt;/code&gt;: Sums up the third column.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;END {print sum}&lt;/code&gt;: Outputs the total sum.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 15: Filtering and Redirecting Output with awk
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Extract error messages from &lt;code&gt;system.log&lt;/code&gt; and save them to &lt;code&gt;errors.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Log File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Timestamp] [Error Level] [Message]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'/ERROR/ {print &amp;gt; "errors.log"}'&lt;/span&gt; system.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/ERROR/&lt;/code&gt;: Filters lines containing 'ERROR'.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{print &amp;gt; "errors.log"}&lt;/code&gt;: Redirects them to &lt;code&gt;errors.log&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 16: sed for Adding Text Based on a Pattern
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Add a warning message after every error line in &lt;code&gt;logs.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'/ERROR/i\Warning: Check the error above.'&lt;/span&gt; logs.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/ERROR/i\...&lt;/code&gt;: Inserts the warning message before lines containing 'ERROR'.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 17: awk for Conditional Field Modification
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; In &lt;code&gt;report.csv&lt;/code&gt;, double the value in the second column if the first column is 'A'.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSV File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Category,Value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;, &lt;span class="s1"&gt;'$1 == "A" {$2 *= 2} {print}'&lt;/span&gt; report.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$1 == "A" {$2 *= 2}&lt;/code&gt;: Doubles the second field if the first is 'A'.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 18: Combining awk and sed for Complex File Editing
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Extract lines containing 'DEBUG' from &lt;code&gt;dev.log&lt;/code&gt;, and remove timestamps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Commands:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'/DEBUG/ {print}'&lt;/span&gt; dev.log | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s/^\[[0-9-: ]*\] //'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;awk '/DEBUG/ {print}'&lt;/code&gt;: Filters lines with 'DEBUG'.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sed 's/^\[[0-9-: ]*\] //'&lt;/code&gt;: Removes the timestamp pattern.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 19: sed for Inline Commenting in Code Files
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Comment out lines containing 'deprecate' in &lt;code&gt;code.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'/deprecate/s/^/#/'&lt;/span&gt; code.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/deprecate/s/^/#/&lt;/code&gt;: Prepends &lt;code&gt;#&lt;/code&gt; to lines containing 'deprecate'.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 20: awk for Extracting and Reporting Unique Values
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Find and count unique user IDs in &lt;code&gt;users.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Log File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UserID,Action
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;, &lt;span class="s1"&gt;'{users[$1]++} END {for (user in users) print user, users[user]}'&lt;/span&gt; users.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**Explanation:&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;{users[$1]++}&lt;/code&gt;: Counts occurrences of each UserID.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;END {...}&lt;/code&gt;: Prints each UserID and its count.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 21: Parsing JSON-like Logs
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Extract values from a JSON-like log file, &lt;code&gt;json.log&lt;/code&gt;, where each line is a JSON object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Log File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"date": "2024-01-10", "event": "login", "user": "user1"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;&lt;span class="s1"&gt;'[,:}]'&lt;/span&gt; &lt;span class="s1"&gt;'{for (i=1; i&amp;lt;=NF; i++) if ($i ~ /"user"/) print $(i+1)}'&lt;/span&gt; json.log | &lt;span class="nb"&gt;tr&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;' "'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-F'[,:}]'&lt;/code&gt;: Sets field separator to handle JSON structure.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;if ($i ~ /"user"/)&lt;/code&gt;: Searches for "user" field.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;tr -d ' "'&lt;/code&gt;: Removes extra characters.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 22: Extracting Data from Nested Log Structures
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Parse nested data structures in &lt;code&gt;complex.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Log File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[main:[subkey:value, ...], otherinfo:...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;&lt;span class="s1"&gt;'[,:[]'&lt;/span&gt; &lt;span class="s1"&gt;'{for (i=1; i&amp;lt;=NF; i++) if ($i ~ /subkey/) print $(i+1)}'&lt;/span&gt; complex.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-F'[,:[]'&lt;/code&gt;: Custom field separators for nested structure.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;if ($i ~ /subkey/)&lt;/code&gt;: Identifies desired nested key.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 23: sed for Conditional Replacement in Files
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; In &lt;code&gt;settings.conf&lt;/code&gt;, replace 'enabled' with 'disabled' only in lines containing 'featureX'.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'/featureX/s/enabled/disabled/'&lt;/span&gt; settings.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/featureX/s/enabled/disabled/&lt;/code&gt;: Conditional pattern replacement.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 24: awk for Generating Reports with Headers and Footers
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Generate a report from &lt;code&gt;data.csv&lt;/code&gt; with a header and footer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSV File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name,Value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;, &lt;span class="s1"&gt;'BEGIN {print "Report Header"} {print $1, $2} END {print "Report Footer"}'&lt;/span&gt; data.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;BEGIN {print "Report Header"}&lt;/code&gt;: Prints header.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;END {print "Report Footer"}&lt;/code&gt;: Prints footer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 25: sed for File Content Reversal
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Reverse the order of lines in &lt;code&gt;file.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'1!G;h;$!d'&lt;/span&gt; file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;'1!G;h;$!d'&lt;/code&gt;: Complex command to reverse line order.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 26: awk for Data Range Selection
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Select records from &lt;code&gt;records.csv&lt;/code&gt; within a value range.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSV File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ID,Value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;, &lt;span class="s1"&gt;'$2 &amp;gt;= 100 &amp;amp;&amp;amp; $2 &amp;lt;= 200'&lt;/span&gt; records.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$2 &amp;gt;= 100 &amp;amp;&amp;amp; $2 &amp;lt;= 200&lt;/code&gt;: Selects rows with second column in specified range.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 27: sed for Selective Line Number Printing
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Print specific line numbers from &lt;code&gt;file.txt&lt;/code&gt;, for example, line 10 to 20.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &lt;span class="s1"&gt;'10,20p'&lt;/span&gt; file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-n '10,20p'&lt;/code&gt;: Prints lines 10 to 20.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 28: awk for Field Rearrangement
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Rearrange the fields in &lt;code&gt;data.csv&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSV File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Field1,Field2,Field3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;, &lt;span class="s1"&gt;'{print $3, $1, $2}'&lt;/span&gt; data.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;{print $3, $1, $2}&lt;/code&gt;: Changes field order.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 29: sed for Inserting Lines After a Match
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; In &lt;code&gt;script.sh&lt;/code&gt;, insert a new command after each 'echo' command.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'/echo/a\new_command'&lt;/span&gt; script.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/echo/a\new_command&lt;/code&gt;: Inserts 'new_command' after lines containing 'echo'.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 30: awk for Selective Field Capitalization
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Capitalize the first field in &lt;code&gt;names.csv&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSV File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;firstname,lastname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;, &lt;span class="s1"&gt;'{print toupper($1), $2}'&lt;/span&gt; names.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;toupper($1)&lt;/code&gt;: Capitalizes the first field.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 31: Parsing Apache Log Files
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Extract and list unique visiting IPs from &lt;code&gt;access.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $1}'&lt;/span&gt; access.log | &lt;span class="nb"&gt;sort&lt;/span&gt; &lt;span class="nt"&gt;-u&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$1&lt;/code&gt; extracts the first field, typically the IP address.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sort -u&lt;/code&gt; sorts and lists unique entries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 32: Text Wrapping in Paragraphs
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Wrap each paragraph in &lt;code&gt;text.txt&lt;/code&gt; to 80 characters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'BEGIN{RS=""; FS="\n"} {gsub(/.{80}/,"&amp;amp;\n",$0); print}'&lt;/span&gt; text.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;RS=""&lt;/code&gt; treats blank lines as record separators.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;gsub(/.{80}/,"&amp;amp;\n",$0)&lt;/code&gt; wraps lines at 80 characters.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 33: Incrementing Numbers in File
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Increment all numbers by 1 in &lt;code&gt;data.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s/\([0-9]\+\)/\1+1/'&lt;/span&gt; data.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\([0-9]\+\)&lt;/code&gt; matches numbers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\1+1&lt;/code&gt; increments the matched numbers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 34: Extracting Email Addresses
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Extract email addresses from &lt;code&gt;contacts.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{for(i=1;i&amp;lt;=NF;i++) if ($i ~ /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/) print $i}'&lt;/span&gt; contacts.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Regular expression matches email addresses.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 35: Converting CSV to Tab-Delimited
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Convert &lt;code&gt;data.csv&lt;/code&gt; to tab-delimited format.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'BEGIN{FS=","; OFS="\t"} {print $1,$2,$3}'&lt;/span&gt; data.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;FS=","&lt;/code&gt; sets the field separator to a comma.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;OFS="\t"&lt;/code&gt; sets the output field separator to a tab.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 36: Removing Duplicate Lines
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Remove duplicate lines in &lt;code&gt;file.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'!seen[$0]++'&lt;/span&gt; file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;!seen[$0]++&lt;/code&gt; tracks and filters out duplicates.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 37: Counting Word Frequency
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Count the frequency of each word in &lt;code&gt;text.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{for(i=1;i&amp;lt;=NF;i++) words[$i]++} END{for(word in words) print word, words[word]}'&lt;/span&gt; text.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Counts and prints the frequency of each word.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 38: In-place File Editing
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Replace 'oldtext' with 'newtext' in &lt;code&gt;file.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/oldtext/newtext/g'&lt;/span&gt; file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In-place substitution of text.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 39: Summarizing Disk Usage
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Summarize disk usage from &lt;code&gt;df&lt;/code&gt; output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;df&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'NR&amp;gt;1 {sum += $3} END {print "Total used:", sum}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sums the third column representing disk usage.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 40: Sed for Line Numbering
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Add line numbers to &lt;code&gt;file.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; file.txt | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'N;s/\n/ /'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adds line numbers before each line.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 41: Joining Lines Based on Pattern
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Join lines ending with a backslash in &lt;code&gt;script.sh&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;':a; /\\$/N; s/\\\n//; ta'&lt;/span&gt; script.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Joins lines ending with a backslash.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 42: Multi-file Processing with awk
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Calculate the total of the third column across multiple CSV files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'BEGIN{FS=","} {total += $3} END{print total}'&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sums the third column of all CSV files.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 43: Filtering Specific Time Range
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Extract log entries from &lt;code&gt;server.log&lt;/code&gt; within a specific hour.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'$2 &amp;gt;= "10:00:00" &amp;amp;&amp;amp; $2 &amp;lt;= "11:00:00"'&lt;/span&gt; server.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Filters entries based on time range.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 44: Random Line Selection
&lt;/h3&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;p&gt;Task:** Select a random line from &lt;code&gt;quotes.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'BEGIN{srand()} {lines[NR]=$0} END{print lines[int(rand()*NR)+1]}'&lt;/span&gt; quotes.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Selects a random line from the file.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 45: Sed for Case Conversion
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Convert all text to uppercase in &lt;code&gt;file.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s/.*/\U&amp;amp;/'&lt;/span&gt; file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Converts each line to uppercase.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 46: Extracting Specific Columns
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Extract the first and third columns from &lt;code&gt;data.csv&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;, &lt;span class="s1"&gt;'{print $1,$3}'&lt;/span&gt; data.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prints the first and third fields.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 47: In-line Comment Removal
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Remove C++ style comments (//) from &lt;code&gt;code.cpp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s|//.*||'&lt;/span&gt; code.cpp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Removes in-line comments.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 48: Splitting a File into Multiple Parts
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Split &lt;code&gt;largefile.txt&lt;/code&gt; into multiple files based on a pattern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'/PATTERN/{n++}{print &amp;gt; "outfile" n ".txt"}'&lt;/span&gt; largefile.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Splits file at each occurrence of 'PATTERN'.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 49: Aggregating Network Traffic Data
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Summarize total bytes transferred from &lt;code&gt;network.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Log File Format:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IP,Bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;awk Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;, &lt;span class="s1"&gt;'{total[$1] += $2} END{for (ip in total) print ip, total[ip]}'&lt;/span&gt; network.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sums bytes for each IP address.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 50: Sed for Conditional File Inclusion
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Include &lt;code&gt;extra.txt&lt;/code&gt; contents in &lt;code&gt;main.txt&lt;/code&gt; after a specific pattern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sed Command:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'/PATTERN/r extra.txt'&lt;/span&gt; main.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inserts &lt;code&gt;extra.txt&lt;/code&gt; contents after 'PATTERN'.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;awk&lt;/code&gt; and &lt;code&gt;sed&lt;/code&gt;, two of the most potent text processing utilities available in Unix-like environments. These tools, with their rich set of features, can handle a vast range of tasks from simple text substitutions to complex data manipulations and analyses. Their usefulness extends across various domains, making them indispensable for system administrators, data scientists, and developers alike. With a little practice, we can master these tools and significantly enhance our productivity and capabilities in data handling.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>productivity</category>
      <category>bash</category>
    </item>
    <item>
      <title>My Comprehensive Car Insurance Claim Journey</title>
      <dc:creator>Amal Shehu</dc:creator>
      <pubDate>Tue, 25 Jul 2023 21:03:09 +0000</pubDate>
      <link>https://dev.to/amalshehu/my-comprehensive-car-insurance-claim-journey-15db</link>
      <guid>https://dev.to/amalshehu/my-comprehensive-car-insurance-claim-journey-15db</guid>
      <description>&lt;p&gt;Hello readers,&lt;/p&gt;

&lt;p&gt;I wanted to share my recent experience with my comprehensive car insurance claim and how it unfolded. The incident happened while I was traveling with my family. A driving school car abruptly stopped in front of us, and I had to apply sudden brakes. However, a lorry coming from behind couldn't stop in time due to worn-out tyres and ended up hitting the back of our car.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reporting The Incident
&lt;/h3&gt;

&lt;p&gt;As the responsible driver, I reported the accident to the police and filed a General Diary (GD). I also documented the scene of the accident and gathered witnesses to support my claim. I informed my insurance company, ICICI Lombard (Policy No: 3001/290417455/00/000), about the incident and filed a claim.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Claim Dispute
&lt;/h3&gt;

&lt;p&gt;The repair estimate given by the service center was INR 55,274. However, my insurance company only approved INR 34,195 under the claim, which was due to the depreciated value of certain car parts. The remaining INR 21,079 was expected to be paid by me out-of-pocket. &lt;/p&gt;

&lt;h3&gt;
  
  
  Legal Action and Insurance Company's Role
&lt;/h3&gt;

&lt;p&gt;I decided to dispute the claim amount by involving legal consultation. The legal experts advised me that the third party (the lorry driver or owner) should be responsible for the accident as per the Motor Vehicles Act, 1988. &lt;/p&gt;

&lt;p&gt;As per the Act, the insurer of the vehicle at fault should compensate for the damages caused by their insured vehicle. However, ICICI Lombard informed me that it is not their responsibility to contact the third party or their insurance company, which is a common practice in India. I was advised to pursue the matter independently or through legal channels.&lt;/p&gt;

&lt;h3&gt;
  
  
  Contacting The Third Party
&lt;/h3&gt;

&lt;p&gt;I contacted the lorry driver, informing him about his legal liability to pay for the damages or to inform his insurance company about the accident. He said he would discuss the matter with the lorry owner and get back to me.&lt;/p&gt;

&lt;h3&gt;
  
  
  Current Situation
&lt;/h3&gt;

&lt;p&gt;Currently, my situation can be best described as a "subrogation claim dispute." Subrogation refers to the right of the insurance company to recover the money paid out in a claim from a third party that was actually at fault. &lt;/p&gt;

&lt;p&gt;As of now, I have not received any response from the lorry driver or owner, and the service center is waiting for the remaining payment for the repair works. I have decided to escalate the matter by filing a formal written complaint to the police station, asking for assistance in recovering the remaining amount from the third party.&lt;/p&gt;

&lt;p&gt;Stay tuned for the next update on how the situation evolves. Remember, understanding your rights and responsibilities is key in navigating such insurance claim disputes.&lt;/p&gt;

</description>
      <category>insurance</category>
      <category>scam</category>
    </item>
  </channel>
</rss>
