How to parse a file for text b/n double quotes?

Hi guys,
I desperately need some help here...
I need to parse a file similar to this:

I need to read the values for MY_BANNER_SSHD and WARNING_MESSAGE. The value could be empty/single line or multi-line!

# Comments
     .
     .
     .

Some lines

MY_BANNER_SSHD=""
WARNING_MESSAGE=""

# Comments
     .
     .
     .

Some lines


 

I have tried-

cat MYHardening.cfg | sed -n '/^MY_BANNER_SSHD=/,/"/p' | sed 's/MY_BANNER_SSHD=//g' | grep -v ^WAR| tr -d \" > tempfile1

cat MYHardening.cfg | sed -n '/^WARNING_MESSAGE=/,/"/p' | sed 's/WARNING_MESSAGE=//g' | tr -d \" > tempfile2

If there are multi-line values in those variables this seems to work fine!
But, when they are null, in tempfile2 I get the trailing part of the file starting after WARNING_MESSAGE="" :frowning:
Can someone help please...

What is the sample format for multiline text? you have shown only empty strings.

try:

awk '/^MY_BANNER_SSHD/ || /^WARNING_MESSAGE/ { split ($0,a,"="); gsub ("\"","",a[2]); print a[2]} ' MYHardening.cfg

^^^

Thanks a ton for quick reply. I want to put some sample banner text into those variables. Something like

WARNING_MESSAGE="This is a
secure system!
All activities will be monitored!"

I tried your suggestion but it would only give
"This is a"

If you could put the message without newline ( let it be exceeds the line width ) it should work.
Is is acceptable for you?

something like..

# Comments
     .
     .
     .
 
Some lines
 
MY_BANNER_SSHD=""
WARNING_MESSAGE="some text"
WARNING_MESSAGE="some text which is too long too fit in single linesome text which is too long too fit in single line some text which is too long too fit in single line some text which is too long too fit in single line"
 
# Comments

Please let us know if you want to stick with your current format.

This could help to extract the value of MY_BANNER_SSHD

sed 's/^MY_BANNER_SSHD=\(.*\)/\1/' inputfile

similarly use WARNING_MESSAGE= in the sed to get its value..

^^^
I'm afraid restricting the text to single line won't help me. :frowning:

This is not quite helping me, it only clears MY_BANNER_SSHD= from my input file :frowning:

But is there a way to use sed or some other utility to read between two characters? In my case the character is ".

Something like this?

awk -F= '
/MY_BANNER_SSHD/ || /WARNING_MESSAGE/ {print $2; if(!/\"$/)f=1 ;next}
f && /\"$/{print; f=0; next}
f' file

Wow!! This worked like charm for me. Thanks a ton :slight_smile:
Here's how I used it...

MY_BANNER_SSHD=`awk -F= '
/^MY_BANNER_SSHD/ {print $2; if(!/\"$/)f=1 ;next}
f && /\"$/{print; f=0; next}
f' MYHardening.cfg | tr -d \"`

echo "MY_BANNER_SSHD=$MY_BANNER_SSHD"

WARNING_MESSAGE=`awk -F= '
/^WARNING_MESSAGE/ {print $2; if(!/\"$/)f=1 ;next}
f && /\"$/{print; f=0; next}
f' MYHardening.cfg | tr -d \"`

echo "WARNING_MESSAGE=$WARNING_MESSAGE"

Thanks to everyone for helping me out :slight_smile: