mardi 15 décembre 2020

Output the value/word after one pattern has been found in string in variable (grep, awk, sed, pearl etc)

I have a program that prints data into the console like so (separated by space):

variable1 value1
variable2 value2
variable3 value3
varialbe4 value4

In the end I want to search e.g. for variable3 but only get value3 as output. I first tryed to put all the console output into a file and process it from there with e.g.

# value3_var="$(grep "variable3" file.log | cut -d " " -f2)"

This works fine but is too slow. I need to process ~20 of these variables per run and this takes ~1-2 seconds on my system. Also I need to do this in ~500 runs. Therefore I thought about putting the console output directly into a variable. But this will then eliminate the newline characters which then again makes it more complicated to process:

# console_output=$(./programm_call)
# echo $console_output
variable1 value1 variable2 value2 variable3 value3 varialbe4 value4

I found a solution for this kind of string arangement, but these seem only to work with a text file. At least I was not able to use the string stored in $console_output with these examples

How to print the next word after a found pattern with grep,sed and awk?

So, how can I output the next word after a found pattern, when providing a (long) string as variable?

PS: grep on my system does not know the parameter -P...

Aucun commentaire:

Enregistrer un commentaire