Menu
Newbedev LogoNEWBEDEV Python Javascript Linux Cheat sheet
Newbedev LogoNEWBEDEV
  • Python 1
  • Javascript
  • Linux
  • Cheat sheet
  • Contact

awk group year and month from date in col1 and sum of the group in col2

Try this

$ awk '{a[substr($0,0,7)]+=$2}END{for(b in a){print b,a[b]}}' myfile
2018-02 22
2019-01 213
2018-03 13
2018-04 9
$

For sorted, add sort

$ awk '{a[substr($0,0,7)]+=$2}END{for(b in a){print b,a[b]}}' myfile | sort
2018-02 22
2018-03 13
2018-04 9
2019-01 213
$

Tags:

Awk

Text Processing

Related

How to count words in a line Equivalent to "source" in OpenBSD? Emit zero-width bash prompt sequence from external binary zsh: How to get value of variable based on a another variable? ip vs ifconfig commands pros and cons How do I add a variable to this curl command? What are the advantages of using `make` for small projects? dirname appears not to work with xargs insert EOF statement before the last line of file Checking for the existence of multiple directories Indirectly access environment variable How to escape the null character in here-document?(bash and/or dash)

Recent Posts

Pandas how to find column contains a certain value Recommended way to install multiple Python versions on Ubuntu 20.04 Build super fast web scraper with Python x100 than BeautifulSoup How to convert a SQL query result to a Pandas DataFrame in Python How to write a Pandas DataFrame to a .csv file in Python
© 2021 newbedevPrivacy Policy