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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to parse a file for text b/n double quotes?
# 1  
Old 10-08-2010
Error 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!

Code:
# Comments
     .
     .
     .

Some lines

MY_BANNER_SSHD=""
WARNING_MESSAGE=""

# Comments
     .
     .
     .

Some lines

I have tried-
Code:
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="" Smilie
Can someone help please...
# 2  
Old 10-08-2010
What is the sample format for multiline text? you have shown only empty strings.

try:

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

# 3  
Old 10-08-2010
^^^

Thanks a ton for quick reply. I want to put some sample banner text into those variables. Something like
Code:
WARNING_MESSAGE="This is a
secure system!
All activities will be monitored!"

I tried your suggestion but it would only give
"This is a"
# 4  
Old 10-08-2010
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..

Code:
# 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.
# 5  
Old 10-08-2010
This could help to extract the value of MY_BANNER_SSHD

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

similarly use WARNING_MESSAGE= in the sed to get its value..
# 6  
Old 10-08-2010
Quote:
Originally Posted by anchal_khare
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..

Code:
# 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.
^^^
I'm afraid restricting the text to single line won't help me. Smilie

Quote:
Originally Posted by michaelrozar17
This could help to extract the value of MY_BANNER_SSHD

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

similarly use WARNING_MESSAGE= in the sed to get its value..
This is not quite helping me, it only clears MY_BANNER_SSHD= from my input file Smilie

But is there a way to use sed or some other utility to read between two characters? In my case the character is ".
# 7  
Old 10-08-2010
Something like this?
Code:
awk -F= '
/MY_BANNER_SSHD/ || /WARNING_MESSAGE/ {print $2; if(!/\"$/)f=1 ;next}
f && /\"$/{print; f=0; next}
f' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk -File contain double quotes

Hi ALL, file data like : test.csv a,b,"c,d" my awk version is 4.0.2 ,if i am using the below code is working fine. awk -vFPAT='(*)|("+")' -vOFS="," '{print $3}' test.csv if the awk version is 3.1.7 is not working . Could you please help me on this one. output should be : "c,d" (6 Replies)
Discussion started by: bmk123
6 Replies

2. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

3. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

4. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

5. Shell Programming and Scripting

File with double quotes

I have one file a.txt as below. a.txt "aaas","111111","ewwee32e","deee333", "aaas","111111","ewwee32e","deee333", "aaas","111111","ewwee32e","deee333", "aaas","111111","ewwee32e","deee333", I want to write a script to process a.txt and want the output as below in different file as below -... (2 Replies)
Discussion started by: ravigupta2u
2 Replies

6. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

7. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

8. UNIX for Dummies Questions & Answers

Removing double quotes in a file

Hi All, I have a tab delimited file where each of the strings have double quotes. The problem is that I have records which are in the following format: "TEXAS" ""HOUSTON"" "123" "" "2625-39-39" ""MAINE"" "" "456" "I" "3737-39-82" I would have to output... (3 Replies)
Discussion started by: kingofprussia
3 Replies

9. Shell Programming and Scripting

Parse comma delimited and optionally quotes dimilited file

Hi, Can you please help me? How to Parse a comma delimited and optionally quotes dimilited file? sample.dat ---------- "I",+2007,"SANDA, 20, MARTIN PLACE","SANDA 20MARTIN" "D",+2008,"RANDA, 22, MARTIN PLACE","RANDA 22MARTIN" Thank you. Ram (8 Replies)
Discussion started by: vramana2003
8 Replies

10. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies
Login or Register to Ask a Question