InfoHeap
Tech tutorials, tips, tools and more
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 sum examples

By admin on Jan 14, 2016

Here are some awk examples to sum an integer column from a file or stdin (standard input) stream.

Data used in these examples

1
2
badval
4

Awk sum example on command line

Lines with invalid numbers are treated as 0. To sum values from stdin use the following command:

cat data1.txt | awk '{ S+=$1} END {print S}'
7
Env: GNU bash, version 4.2.46

To sum values from file use the following command:

awk '{ S+=$1} END {print S}' data1.txt
7
Env: GNU bash, version 4.2.46

Bash alias for awk sum code

Put the following line in your ~/.bash_profile

alias mysum="awk '{ S+=\$1} END {print S}'"

When you login next time, you can simply run

$ mysum data1.txt
##or 
$ cat data1.txt | mysum

Suggested posts:

  1. awk – if condition examples
  2. Bash – how to redirect stderr to stdout or file
  3. Bash – pass all arguments from one script to another
  4. Bash – iterate over array
  5. Perl command line – replace multi line comments
  6. Bash – how to find last command exit status code
  7. Bash check if file begins with a string
  8. Bash – how to use functions – quick tutorial
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

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | 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

Copyright © 2022 InfoHeap.

Powered by WordPress