Skip to main content

Command Palette

Search for a command to run...

Linux commands for awk

Published
•2 min read•View as Markdown
A

I'm a DevOps & Cloud Engineer and a passionate technical blogger. I share insights and best practices on DevOps, cloud architecture, CI/CD, automation, and more. Let's explore the world of efficient software delivery and scalable cloud infrastructure together.

Using AWK command in Bash on Linux | A Cloud Guru

  1. Printing and Filtering:

    • awk '{print}' file: Print the entire file.

    • awk '/pattern/ {print}' file: Print lines matching a pattern.

    • awk '{print $1, $2}' file: Print specific columns (e.g., 1st and 2nd columns).

    • awk '$3 > 10 {print}' file: Print lines where the 3rd column is greater than 10.

    • awk 'length($0) > 80 {print}' file: Print lines longer than 80 characters.

      1. Variables and Arithmetic:

        • awk '{sum += $1} END {print sum}' file: Calculate and print the sum of values in a column.

        • awk '{avg += $1; count++} END {print avg/count}' file: Calculate and print the average of values in a column.

        • awk '{if ($1 > 10) print $1; else print "N/A"}' file: Use conditional statements to control printing.

        • awk '{total = $2 * $3; print total}' file: Perform arithmetic operations on columns.

      2. Formatting and Output:

        • awk '{printf "%-10s %5d\n", $1, $2}' file: Format and print columns with specific widths.

        • awk '{print NR, $0}' file: Print line numbers with each line.

        • awk 'BEGIN {FS=","; OFS="\t"} {print $1, $2}' file: Set field separators (input and output) to customize data processing.

      3. Regular Expressions:

        • awk '/^pattern/ {print}' file: Print lines starting with a specific pattern.

        • awk '/pattern$/ {print}' file: Print lines ending with a specific pattern.

        • awk '/pattern1/ && /pattern2/ {print}' file: Print lines matching multiple patterns.

          Rhonda Byrne Quote: “There is always something to be grateful for.”

More from this blog

Anurag's Blog

18 posts

I'm Anurag, a DevOps engineer and technical blogger at HasNode, merging Cloud Technology and DevOps for seamless software delivery.