• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

awk Command Examples in Linux

by admin

The awk command performs pattern matching on files. It is based on the AWK programming language. The awk keyword is followed by the pattern, the action to be performed, and the file name. The action to be performed is given within curly braces. The pattern and the action to be performed should be specified within single quotes. If the pattern is not specified, the action is performed on all input data; however, if the action is not specified, the entire line is printed. The awk command can be executed from the command-line or from within an awk script file.

The awk command can be used to process text files in a variety of ways, such as extracting text matching a certain pattern; deleting text matching a certain pattern; adding text matching a certain pattern; and much more.

Syntax

The syntax of the awk command is:

# awk [options] ['patterns {actions}'] {file names}

awk Command Examples

1. Print the fifth column (a.k.a. field) in a space-separated file:

# awk '{print $5}' filename

2. Print the second column of the lines containing “foo” in a space-separated file:

# awk '/foo/ {print $2}' filename

3. Print the last column of each line in a file, using a comma (instead of space) as a field separator:

# awk -F ',' '{print $NF}' filename

4. Sum the values in the first column of a file and print the total:

# awk '{s+=$1} END {print s}' filename

5. Print every third line starting from the first line:

# awk 'NR%3==1' filename

6. Print different values based on conditions:

# awk '{if ($1 == "foo") print "Exact match foo"; else if ($1 ~ "bar") print "Partial match bar"; else print "Baz"}' filename

7. Print all lines where the 10th column value equals the specified value:

# awk '($10 == value)'

8. Print all the lines which the 10th column value is between a min and a max:

# awk '($10 >= min_value && $10 <= max_value)'

Filed Under: Linux

Some more articles you might also be interested in …

  1. arduino: Arduino Studio – Integrated Development Environment for the Arduino platform
  2. mount: command not found
  3. How to allow only specific non-root user(s) to use crontab
  4. Glusterfs – Advanced Troubleshooting Tips and Tricks
  5. kdialog Command Examples in Linux
  6. csvsql: Generate SQL statements for a CSV file or execute those statements directly on a database
  7. How to install CentOS / RHEL 7 on RAID Partition
  8. Basic Linux File system tutorial – ext2, ext3, ext4, JFS and XFS
  9. How To Add/Remove Locale Archive in CentOS/RHEL 5,6
  10. How to Change the VNC Server Resolution in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • ctags: Generates an index (or tag) file of language objects found in source files for many popular programming languages
  • csvtool: Utility to filter and extract data from CSV formatted sources
  • csvstat: Print descriptive statistics for all columns in a CSV file
  • csvsql: Generate SQL statements for a CSV file or execute those statements directly on a database

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright