Glam Prestige Journal

Bright entertainment trends with youth appeal.

I have this single line string.

WordPress version: 5.5.3 Database revision: 48748 TinyMCE version: 4.9100 (49100-20200624) Package language: en_US

I would like to extract 5.5.3 and en_US

This is the closest example I am able to come up with where a space is the delimiter.

`awk -F" " '{print $3 $13 }' <<< 'WordPress version: 5.5.3 Database revision: 48748 TinyMCE version: 4.9100 (49100-20200624) Package language: en_US'`

which returns

5.5.3en_US

I have looked at other examples and I am not getting anywhere. I would prefer to not rely on any counts ($3 and $13) and take the first value after a substring. In this case, WordPress version: and Package language:

Putting these two variables into an array would be awesome. Appreciate any help.

1

1 Answer

If awk is no requirement you could just cut string into substrings with grep,
replace colons with = and whitespaces with _ with sed.
The output could evaluated with eval to make it variables

WordPress version: 5.5.3 -> WordPress_version=5.5.3
Database revision: 48748 -> Database_revision=48748
TinyMCE version: 4.9100 -> TinyMCE_version=4.9100
Package language: en_US -> Package_language=en_US

to make this work you need to differ variable names from variable values.
in this example this is done by len so en_US is treated as value

#!/bin/bash
# input string
text='WordPress version: 5.5.3 Database revision: 48748 TinyMCE version: 4.9100 (49100-20200624) Package language: en_US'
# variable length
len=6
# find variable names based on length
for var in $(echo "$text" | grep -oE "[^(0-9:)]{$((len+1)),}" | grep -oE '\S.*' | sed -n 's,\s,.,p') do # convert substrings and set variables name=value eval "$(echo "$text" | grep -oE "$var:\s+\S+" | sed -nr 's,:\s+,=,p' | sed -n 's,\s,_,p')"
done
# print variable info
declare -p | grep -E 'declare -- [A-Za-z]+_\S+='

Note: eval is just used for demonstration. the above code is compatible with (da)sh. in bash you can create array instead of multiple variables

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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