Skip to main content
modification of search pattern
Source Link
Wastrel
  • 143
  • 6

There is nothing really wrong with your grep and cut command. You could make it more robust by using "|30201|" as the search pattern. The issue then is dealing with the output.

Using bash:

#!/bin/bash # get the output as a bash array and add the elements nums=( $(grep "30201""|30201|" logfile.txt | cut -f6 -d "|") ) total=0 for i in ${!nums[@]} do total=$(($total+${nums[i]})) done echo $total 

There is nothing wrong with your grep and cut command. The issue is dealing with the output.

Using bash:

#!/bin/bash # get the output as a bash array and add the elements nums=( $(grep "30201" logfile.txt | cut -f6 -d "|") ) total=0 for i in ${!nums[@]} do total=$(($total+${nums[i]})) done echo $total 

There is nothing really wrong with your grep and cut command. You could make it more robust by using "|30201|" as the search pattern. The issue then is dealing with the output.

Using bash:

#!/bin/bash # get the output as a bash array and add the elements nums=( $(grep "|30201|" logfile.txt | cut -f6 -d "|") ) total=0 for i in ${!nums[@]} do total=$(($total+${nums[i]})) done echo $total 
Source Link
Wastrel
  • 143
  • 6

There is nothing wrong with your grep and cut command. The issue is dealing with the output.

Using bash:

#!/bin/bash # get the output as a bash array and add the elements nums=( $(grep "30201" logfile.txt | cut -f6 -d "|") ) total=0 for i in ${!nums[@]} do total=$(($total+${nums[i]})) done echo $total