I have a text which looks like -
Application.||dates:[2022-11-12]|models:[MODEL1]|count:1|ids:2320
Application.||dates:[2022-11-12]|models:[MODEL1]|count:5|ids:2320
I want the number from the count:1 columns so 1 and i wish to store these numbers in an array.
nums=($(echo -n "$grepResult" | awk -F ':' '{ print $4 }' | awk -F '|' '{ print $1 }'))
this seems very repetitive and not very efficient, any ideas how to simplify this ?
You can use awk once, set the field separator to
|. Then loop all the fields and split on:If the field starts with
countthen print the second part of the splitted value.This way the
count:part can occur anywhere in the string and can possibly print this multiple times.Output
If you want to combine the both split values, you can use
[|:]as a character class and print field number 8 for a precise match as mentioned in the comments.Note that it does not check if it starts with
count:With
gnu awkyou can use a capture group to get a bit more precise match where on the left and right can be either the start/end of string or a pipe char. The 2nd group matches 1 or more digits: