Parse Multi-Section Configuration File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse Multi-Section Configuration File
# 8  
Old 02-27-2010
Because of the spaces?

Maybe in simple shell is easier...

Code:
$ cat Test
SECTION=${1:-"STATION"}
X=0
while IFS=\= read LVAL RVAL; do
  case "$LVAL" in
   "[$SECTION]") P=1; continue;;
   "["*) P=0; continue
  esac
  [ P -eq 1 ] && VALUE[$X]=$RVAL && X=$((X + 1))
done < file1

echo All:  ${VALUE[@]}
echo Val1: ${VALUE[0]}
echo Val2: ${VALUE[1]}
echo Val3: ${VALUE[2]}

$ ./Test
All:  NL NLDOT "Chateau Pond" NLPCP NLDOT_NLPCP
Val1: NL
Val2: NLDOT
Val3: "Chateau Pond"

Or using the original sed:
Code:
$ cat Test
X=0
sed -n "/\[STATION]/,/\[/ {/\[/d;s/.* *= *//;p;}" file1 | while read LINE; do
  A[$X]=$LINE
  X=$((X + 1))
done

echo ${A[@]}
echo ${A[0]}
echo ${A[1]}
echo ${A[2]}

$ ./Test
NL NLDOT "Chateau Pond" NLPCP NLDOT_NLPCP
NL
NLDOT
"Chateau Pond"


Last edited by Scott; 02-27-2010 at 05:31 PM.. Reason: Updated example
# 9  
Old 02-28-2010
I tried the sed version but encountering two problems

Code:
	X=0
	sed -n "/\[NETWORK-CAMERA.C1]/,/\[/ {/\[/d;s/.* *= *//;p;}" ../etc/app.conf | while read LINE; do 
		A[$X]=$LINE 
		echo ${A[$X]} 
		X=$((X + 1)) 
		done
	#
	echo ${A[@]}

I can see the values in the array while it loops but when I try to see all (@) it is empty. The other issue is with path values having more than one / slash. I assume it is with the reg expr being used but I cant figure out why the array is empty.
# 10  
Old 02-28-2010
In bash the pipe | before while creates a sub shell. One of your records has an = sign in the value part, that's confusing it.

Code:
  while read LINE; do
    A[$X]=$LINE
    echo ${A[$X]}
    X=$((X + 1))
    done < <(sed -n "/\[STATION]/,/\[/ {/\[/d;s/.* *= //;p;}" file1)
  #
  echo ${A[@]}

# 11  
Old 02-28-2010
Have you tried the solution of danmero?

Quote:
Originally Posted by danmero
For old shell try:
Code:
#!/bin/sh
set -- $(awk '/\[DATA-CENTER.S1]/{f=1;next}/\[/{f=0}f{print $NF}' file)
echo $@
echo $1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse section of csv into array

In the awk below I am trying to parse the Sample Name below the section. The values that are extracted are read into array s(each value in a row seperated by a space) which will be used later in a bash script. The awk does execute but no values are printed. I am also not sure how to print in a row... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Xml multi parse question

awk -F "" '/<Id>|<id>|<Source>|<source>|<Accession>|<accession>|<TestName>|<testname>/ {print $2, $3}' OFS='\t' Test.xml The above code works great, but lets say I wanted the <Analyte> name (<Name>STAT3). The word <name> is unique so there will be multiple records pulled. Is there a way to... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

How to substract selective values in multi row, multi column file (using awk or sed?)

Hi, I have a problem where I need to make this input: nameRow1a,text1a,text2a,floatValue1a,FloatValue2a,...,floatValue140a nameRow1b,text1b,text2b,floatValue1b,FloatValue2b,...,floatValue140b look like this output: nameRow1a,text1b,text2a,(floatValue1a - floatValue1b),(floatValue2a -... (4 Replies)
Discussion started by: nricardo
4 Replies

4. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

5. Shell Programming and Scripting

Delete a section of a file if...

i have a file as below that has n section : 2006 0101 1236 49.3 L 37.902 48.482 0.0 Teh 5 0.2 2.7LTeh 1 GAP=238 E Iranian Seismological Center, Institute of Geophysics, University of Tehran 6 ... (5 Replies)
Discussion started by: oreka18
5 Replies

6. Shell Programming and Scripting

SH script to parse string and return multi-line file

Hello all, I have been asked to exercise my shell scripting and it has been 10 plus years since I used to do it so I can not remember hardly anything and ask for your help. What I need to do is copy a line out of a file that can be 10 to 100 characters long, I then need to parse this line into... (3 Replies)
Discussion started by: Alivadoro
3 Replies

7. Shell Programming and Scripting

Parse configuration file & add line in particular section

Greetings, I recently built a replicated DRBD, Heartbeat, & iSCSI Target Initiator storage server on Ubuntu 10.04 to offer shared storage to server Vmware ESX and Microsoft Clusters. Everything works flawlessly, however I wanted to make a script to create, remove, grow volumes to offer ESX... (6 Replies)
Discussion started by: Aeudian
6 Replies

8. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

9. Shell Programming and Scripting

Search and extract by section from configuration

Hi, I understand either AWK or SED can do this, but I not sure how to extract the following configuration in section. Meaning when I need to find code with " ip helper-address 192.168.11.2" , it would start from "interface Serial0/0" and "interface FastEthernet0/1". Only displaying both section... (2 Replies)
Discussion started by: haphazard
2 Replies

10. UNIX for Dummies Questions & Answers

multi-file multi-edit

Good day! I am trying to learn how to use the "sed" editor, to perform multiple edits on multiple files in multiple directories. I have one script that tries to call up each file and process it according to the edits listed in a second script. I am using a small input text to test these, at... (12 Replies)
Discussion started by: kielitaide
12 Replies
Login or Register to Ask a Question