I have a bash function, FUNCTION_A who's output looks like this:
ID HELD_SINCE HOLD_REASON
123456.0 7/1 20:40 Peak usage: 2370 megabytes.
123457.0 7/1 20:40 Peak usage: 5008 megabytes.
I then need to grab the ID number and the number after Peak usage: to pass to FUNCTION_B.
If I were running it by hand it would look like:
FUNCTION_B 123456.0 2370
FUNCTION_B 123457.0 5008
I am trying to write a bash script using regular expressions to grab these two numbers and run in the shell for an arbitrary number of output lines of FUNCTION_A. I'm getting stuck at a few different places.
I am not sure how to loop through output line by line, and I'm not sure why the array isn't holding any output, but I want to do something like this
re='([0-9-]+\.[0-9-]+).*Peak usage: ([0-9-]+) megabytes\.'
for line in FUNCTION_A output
if [[ line =~ $re ]]; then
myArray=($(echo s | egrep -Eo re))
eval FUNCTION_B ${myArray[0]} ${myArray[1]}
fi
for-- use awhile readloop instead; see "Looping through the content of a file in Bash" and BashFAQ #1: "How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?"