Solved Bash : parse line with non-specific structure

February 5, 2020 at 07:17:24
Specs: Linux 6, 8GB
So I'm needing to parse a configuration file (using Bash Shell), trying to get value(s) from a parameter. All happens on one line. Sounds easy. But this is a problem: the usage of such a line, is made so that multiple formats are accepted. This is easy for configuring this file, but it is harder to parse. I'll supply an example, this is such a line:

parameter=1000 2000

You see there are in fact different values for 1 parameter. The different values of this variable are separated by at least one whitespace. Pretty easy right ?
Well no, because this is an accepted syntax as well:

parameter = 1000 2000

and this is as well

parameter =1000 2000

So you see where we are going: you cannot trust the whitespaces to be anywhere, except for between the values.

It's also not impossible this is entered:

parameter=1000 2000 3000

And also in that case, I need to end up with something like:
variable1 - 1000
variable2 - 2000

I don't need to get the value of the third parameter, if that one is stored. But the point is that there may be extra values listed.

I was trying this first:
sed "s/ //g" filename.dat | grep "^${p_variable}=" | awk -F"=" ' { printf $2 } '

But the problem is that the SED command, messes up the whitespace between value 1 and 2.
That left me with an issue, as I'm not sure how to solve that in an easy manner.

Note that this can also be an entry in the DAT file:

parameter=0 300

Or this:

parameter=1234567890 10

Or this:


Or even this


See More: Bash : parse line with non-specific structure

February 6, 2020 at 08:14:00
Don't have a solution, been too long since I played with Bash, but you will
probably need to pre process the data, IE

use the Equal sign and grab everything following,
then remove any excess space characters with SED or maybe TR
leaving only one space where there were several,
now, since you have eliminated all excess spaces,
you should be able to parse the results without too much difficulty.

You may be able to use AWK for the whole thing, like I said it's been awhile. :-)


Report •

February 14, 2020 at 03:28:30
✔ Best Answer
Got it - this one removes all whitespaces but only the one before my variable keyword, the ones between that word and =, and the ones between = and the value:

cat example.dat | sed "s/[ ]*=/=/g" | sed "s/=[ ]*/=/g" | grep "^[ ]*variable1\=" | awk -F"=" ' { printf "variable=\"" $2 "\"\n" } '

Report •
Related Solutions

Ask Question