Skip to main content
Tweeted twitter.com/StackUnix/status/1117759671715139589
edited title
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 266

How to grep and cut numbesnumbers from a file and sum them

Became Hot Network Question
added 6 characters in body; edited tags
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 266

I have a log file. EveryFor every line with a specific number, I want to sum the last number of this linethose lines. To grep and cut is no problem but I don't know how to sum the numbers. I tried some solutions from StackExchange but didn't get them to work in my case.

This is what I have so far:

grep "30201" logfile.txt | cut -f6 -d "|" 

30201 are the lines I'm looking for.

I wan'twant to sum the last numbers 650, 1389 and 945

The logfile.txt

Jan 09 2016|09:15:17|30201|1|SL02|650 Jan 09 2016|09:15:18|43097|1|SL01|945 Jan 09 2016|09:15:19|28774|2|SB03|1389 Jan 09 2016|09:16:21|00788|1|SL02|650 Jan 09 2016|09:17:25|03361|3|SL01|945 Jan 09 2016|09:17:33|08385|1|SL02|650 Jan 09 2016|09:18:43|10234|1|SL01|945 Jan 09 2016|09:21:55|00788|1|SL02|650 Jan 09 2016|09:24:43|03361|3|SB03|1389 Jan 09 2016|09:26:01|30201|1|SB03|1389 Jan 09 2016|09:26:21|28774|2|SL02|650 Jan 09 2016|09:26:25|00788|1|SL02|650 Jan 09 2016|09:27:21|28774|2|SL02|650 Jan 09 2016|09:29:32|30201|1|SL01|945 Jan 09 2016|09:30:12|34032|1|SB03|1389 Jan 09 2016|09:30:15|08767|3|SL02|650 

I have a log file. Every line with a specific number I want to sum the last number of this line. To grep and cut is no problem but I don't know how to sum the numbers. I tried some solutions from StackExchange but didn't get them to work in my case.

This is what I have so far:

grep "30201" logfile.txt | cut -f6 -d "|" 

30201 are the lines I'm looking for.

I wan't to sum the last numbers 650, 1389 and 945

The logfile.txt

Jan 09 2016|09:15:17|30201|1|SL02|650 Jan 09 2016|09:15:18|43097|1|SL01|945 Jan 09 2016|09:15:19|28774|2|SB03|1389 Jan 09 2016|09:16:21|00788|1|SL02|650 Jan 09 2016|09:17:25|03361|3|SL01|945 Jan 09 2016|09:17:33|08385|1|SL02|650 Jan 09 2016|09:18:43|10234|1|SL01|945 Jan 09 2016|09:21:55|00788|1|SL02|650 Jan 09 2016|09:24:43|03361|3|SB03|1389 Jan 09 2016|09:26:01|30201|1|SB03|1389 Jan 09 2016|09:26:21|28774|2|SL02|650 Jan 09 2016|09:26:25|00788|1|SL02|650 Jan 09 2016|09:27:21|28774|2|SL02|650 Jan 09 2016|09:29:32|30201|1|SL01|945 Jan 09 2016|09:30:12|34032|1|SB03|1389 Jan 09 2016|09:30:15|08767|3|SL02|650 

I have a log file. For every line with a specific number, I want to sum the last number of those lines. To grep and cut is no problem but I don't know how to sum the numbers. I tried some solutions from StackExchange but didn't get them to work in my case.

This is what I have so far:

grep "30201" logfile.txt | cut -f6 -d "|" 

30201 are the lines I'm looking for.

I want to sum the last numbers 650, 1389 and 945

The logfile.txt

Jan 09 2016|09:15:17|30201|1|SL02|650 Jan 09 2016|09:15:18|43097|1|SL01|945 Jan 09 2016|09:15:19|28774|2|SB03|1389 Jan 09 2016|09:16:21|00788|1|SL02|650 Jan 09 2016|09:17:25|03361|3|SL01|945 Jan 09 2016|09:17:33|08385|1|SL02|650 Jan 09 2016|09:18:43|10234|1|SL01|945 Jan 09 2016|09:21:55|00788|1|SL02|650 Jan 09 2016|09:24:43|03361|3|SB03|1389 Jan 09 2016|09:26:01|30201|1|SB03|1389 Jan 09 2016|09:26:21|28774|2|SL02|650 Jan 09 2016|09:26:25|00788|1|SL02|650 Jan 09 2016|09:27:21|28774|2|SL02|650 Jan 09 2016|09:29:32|30201|1|SL01|945 Jan 09 2016|09:30:12|34032|1|SB03|1389 Jan 09 2016|09:30:15|08767|3|SL02|650 
Source Link

How to grep and cut numbes from a file and sum them

I have a log file. Every line with a specific number I want to sum the last number of this line. To grep and cut is no problem but I don't know how to sum the numbers. I tried some solutions from StackExchange but didn't get them to work in my case.

This is what I have so far:

grep "30201" logfile.txt | cut -f6 -d "|" 

30201 are the lines I'm looking for.

I wan't to sum the last numbers 650, 1389 and 945

The logfile.txt

Jan 09 2016|09:15:17|30201|1|SL02|650 Jan 09 2016|09:15:18|43097|1|SL01|945 Jan 09 2016|09:15:19|28774|2|SB03|1389 Jan 09 2016|09:16:21|00788|1|SL02|650 Jan 09 2016|09:17:25|03361|3|SL01|945 Jan 09 2016|09:17:33|08385|1|SL02|650 Jan 09 2016|09:18:43|10234|1|SL01|945 Jan 09 2016|09:21:55|00788|1|SL02|650 Jan 09 2016|09:24:43|03361|3|SB03|1389 Jan 09 2016|09:26:01|30201|1|SB03|1389 Jan 09 2016|09:26:21|28774|2|SL02|650 Jan 09 2016|09:26:25|00788|1|SL02|650 Jan 09 2016|09:27:21|28774|2|SL02|650 Jan 09 2016|09:29:32|30201|1|SL01|945 Jan 09 2016|09:30:12|34032|1|SB03|1389 Jan 09 2016|09:30:15|08767|3|SL02|650