1

This is not a homework assignment. I am new to bash and trying to gather some data from our logging.

I am reading lines from a file. All of the lines look like this:

[info] 1 - 12-04 15:33:37.542 : Finished createWalletRandom, total time 9898ms

I need to parse out the milliseconds (which I will get min, max, average). I can get 9898ms and I need to get rid of the ms for the math to work.

Trying this line below, doesn't change anything:

MILLI_SECONDS=${RAW_MILLI_SECONDS%??}

And trying this below, generates an error get_wallet_times.sh: line 23: -2: substring expression < 0:

MILLI_SECONDS=${RAW_MILLI_SECONDS::-2}

Here is my code:

while read ONE_LINE;
do
    echo $ONE_LINE
    RAW_MILLI_SECONDS="$(cut -d' ' -f13 <<<"$ONE_LINE")"
    echo $RAW_MILLI_SECONDS
    MILLI_SECONDS=${RAW_MILLI_SECONDS::-2}
    MILLI_SECONDS=${RAW_MILLI_SECONDS%??}
    echo ${MILLI_SECONDS}
    LINE_COUNT=$((LINE_COUNT+1))
    FILE_SUM=$((FILE_SUM+MILLI_SECONDS))
done < logfile.txt

This is on macOS, in case its a bash issue specific to mac. Please let me know if there is anything thing else you need.

Thnx Matt

4
  • Does MILLI_SECONDS=${RAW_MILLI_SECONDS/ms/} work? – AdminBee Dec 5 '19 at 16:32
  • yes it does. thank you. if you will, please make that an answer :) – just_a_programmer Dec 5 '19 at 16:33
  • Maybe there are additional characters (spaces?) after ms so %?? removes two of them. What is the output of echo "${RAW_MILLI_SECONDS}X"? – Kamil Maciorowski Dec 5 '19 at 16:35
  • @KamilMaciorowski I wondered that as well. I used a line echo "|${RAW_MILLI_SECONDS}|" and got back |9898ms|. So I don't think theres anything trailing, unless I did that wrong. – just_a_programmer Dec 5 '19 at 16:39
0

I'm unsure if there are differences to the MacOS implementation of bash, but you could try RegExp-based string modification as in

MILLI_SECONDS=${RAW_MILLI_SECONDS/ms/}

which will replace the pattern ms by the empty string.

0

You can use awk to parse the entire file

awk '
    /ms$/ { ms += $NF }
    END { printf "Total %ld ms from %d record(s)\n", ms, NR }
' <logfile.txt

The first line matches against ms as the last two characters of a line and sums the numeric value of the corresponding field. The END line reports the totals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.