• 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 not found

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}

If you encounter the below error while running the awk command:


you may try installing the below package as per your choice of distribution:

OS Distribution Command
OS X brew install awk
Debian apt-get install gawk
Ubuntu apt-get install gawk
Alpine apk add gawk
Arch Linux pacman -S gawk
Kali Linux apt-get install gawk
CentOS yum install gawk
Fedora dnf install gawk
Raspbian apt-get install gawk

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. Understanding rsyslog Templates
  2. gs: command not found
  3. extrace Command Examples in Linux
  4. mailstat Command Examples in Linux
  5. RHEL 7 – RHCSA Notes – input / output redirection
  6. How to uninstall steam from Ubuntu
  7. iostat Command Examples in Linux
  8. What are DNS Nameserver Types in Linux
  9. CentOS / RHEL : How to configure iptable rules to allow FTP ports 20/21
  10. debman Command Examples in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • protonvpn-cli Command Examples in Linux
  • protonvpn-cli connect Command Examples
  • procs Command Examples in Linux
  • prlimit: command not found

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright