Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux awk Command: Edit File Text

In this tutorial, we'll learn about the awk command in Linux and how to use it for text processing.

  • Introduction to the awk command

awk is a powerful text-processing tool in Linux, used for pattern scanning and processing. It's especially useful for working with structured text data, such as data in columns. awk can perform various tasks, such as filtering, formatting, and performing calculations on text files.

  • Basic syntax of the awk command

The basic syntax of the awk command is:

awk 'pattern { action }' input_file
  • pattern: A regular expression pattern to match lines in the input file.
  • action: A set of commands to be executed for each matched line.
  • input_file: The file to be processed.
  • Basic awk usage

Here's a simple example of using awk to print the contents of a file:

awk '{ print }' input_file

This command will print each line of the input_file.

  • Using field separators

awk can process text data in columns by splitting lines into fields based on a specified field separator. By default, the field separator is a space or a tab.

To print specific fields from a text file, you can use the $ symbol followed by the field number:

awk '{ print $1, $3 }' input_file

This command will print the first and third fields of each line in the input_file.

  • Using conditionals

You can use conditional expressions with awk to filter lines based on specific criteria. For example:

awk '$1 > 10 { print $1, $2 }' input_file

This command will print the first and second fields of each line in the input_file, but only if the value of the first field is greater than 10.

  • Using built-in variables

awk provides built-in variables for various purposes:

  • NF: The number of fields in the current line.
  • NR: The current line number.
  • FS: The field separator.

For example, to print the last field of each line:

awk '{ print $NF }' input_file
  • Performing calculations

awk can perform calculations on text data. For example, to calculate the sum of the values in the first column:

awk '{ sum += $1 } END { print sum }' input_file

This command will add the values of the first field of each line and print the total sum at the end.

  • Using loops

You can use loops in awk to iterate over fields or lines. For example, to reverse the order of fields in each line:

awk '{ for (i = NF; i >= 1; i--) print $i }' input_file

By understanding and using the awk command in Linux, you can efficiently process and manipulate text data, especially structured data in columns. This can be very useful for filtering, formatting, and performing calculations on text files.

  1. Editing text files with awk command:

    • Use awk to process and modify text files.
    awk '{print $1, $3}' input.txt > output.txt
    
  2. Using awk to search and replace in Linux:

    • Perform search and replace operations with awk.
    awk '{gsub(/oldpattern/, "newpattern"); print}' input.txt > output.txt
    
  3. Awk command for column manipulation in text files:

    • Manipulate columns in text files using awk.
    awk '{temp=$2; $2=$3; $3=temp; print}' input.txt > output.txt
    
  4. Filtering and formatting text output with awk:

    • Filter and format text output with awk.
    ps aux | awk '$3 > 50 {print $1, $3}'
    
  5. Awk one-liners for common text editing tasks:

    • Use concise awk one-liners for various tasks.
    awk '{if ($2 > 10) print}' input.txt > output.txt
    
  6. Conditional statements with awk in Linux:

    • Implement conditional statements in awk scripts.
    awk '{if ($NF == "error") print "Error found:", $0}' log.txt
    
  7. Working with fields and delimiters using awk:

    • Manage fields and delimiters in text files with awk.
    awk -F':' '{print $1, $NF}' /etc/passwd
    
  8. Awk regular expressions for pattern matching:

    • Utilize regular expressions for pattern matching with awk.
    awk '/pattern/ {print $0}' input.txt > output.txt
    
  9. Awk command for text extraction in Linux:

    • Extract specific information from text using awk.
    awk '/error/ {print $NF}' log.txt > errors.txt
    
  10. Using variables in awk scripts for text editing:

    • Define and use variables in awk scripts.
    awk '{total += $2} END {print "Total:", total}' data.txt
    
  11. Combining awk with other Linux commands for editing:

    • Integrate awk with other commands for powerful text processing.
    grep "keyword" log.txt | awk '{print $3}' | sort | uniq
    
  12. Awk scripting for batch text processing tasks:

    • Create awk scripts for complex text processing tasks.
    # script.awk
    {if ($2 > 10) print $1, $2}
    
    # Run the script
    awk -f script.awk input.txt > output.txt