InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

awk tutorials

  • awk if condition
  • awk sum
 
  • Home
  • > Tutorials
  • > Linux/Unix Command Line
  • > Awk

awk – if condition examples

By admin on Jan 14, 2016

Awk is pretty powerful command line utility to filter lines from a stream. Here are some awk if condition examples.

Data used in these examples:

1 string1
2 string2
100 string3

Awk if example – print full line based on a field value

Print entries (full line) where first column is greater than certain threshold. Here $0 represents full line.

cat data1.txt | awk '{if ($1 > 1) print $0}'
2 string2
100 string3
Env: GNU bash, version 4.2.46

Awk if example – print specific field based on a field value

Print entries (2nd field only) where first column is greater than certain threshold.

cat data1.txt | awk '{if ($1 > 1) print $2}'
string2
string3
Env: GNU bash, version 4.2.46

Awk if example – conditional sum

Find sum of first column values where column value is less that certain threshold. This can be used to exclude outliers.

cat data1.txt | awk '{if ($1 <= 10) S+=$1} END {print S}'
3
Env: GNU bash, version 4.2.46

Awk if example – multiple statements in if

Find avg of first column values where column value is less that certain threshold.

cat data1.txt | awk '{if ($1 <= 10) {S+=$1;C+=1}} END {print S/C}'
1.5
Env: GNU bash, version 4.2.46

Suggested posts:

  1. Dig – list all dns records
  2. How to install Imagemagick on Ubuntu Linux
  3. How to kill unresponsive ssh session using escape sequence
  4. Vim – how to go back to last edited line/context
  5. Benefits of using Amazon AWS – EC2
  6. Bash check if file begins with a string
  7. awk sum examples
  8. Bash – append text to a variable
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Awk, Bash shell scripting, Linux, Linux/Unix Command Line, Mac, Tutorials, Ubuntu Linux
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress