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 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. Svn – how to diff last committed change
  2. AngularJS format date using date filter
  3. wget handy commands
  4. Mac – show volume icon on menu bar
  5. Vim – highlight and move to matching bracket
  6. Bash – how to redirect stderr to stdout or file
  7. Bash – newline and other escape character in string
  8. How to show environment variable for a process id (pid)
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