x2sh - booster script: correction, please USE THIS!

George Makrydakis gmakmail at gmail.com
Fri Feb 17 15:22:21 PST 2006


Sorry, a minor copy / paste error will eat a line while parsing (the 
$((collectSTOP + 1)) variable) , so please use the one below, thanks:

# ----------------------------------CUT HERE---------------------

#!/bin/bash

# script: XML pseudoparsing booster
# license: GPL 2.x and up
# author: George Makrydakis <gmakmail at gmail.com>


declare -a filearray
declare -a filestore
declare -i linecounter
declare -i collectSTART=0
declare -i collectSTOP=0

declare -a chapterlist=(chapter01 \
			chapter02 \
			chapter03 \
			chapter04 \
			chapter05 \
			chapter06 \
			chapter07 \
			chapter09);

for selectchapter in ${chapterlist[@]}
do
	cd $selectchapter
	echo $selectchapter
	for filenameinput in *.xml
	do
		echo "-------------------------------------------------"
		echo "x2sh:parsing file: "$filenameinput
		echo "-------------------------------------------------"	
	while read filearray[linecounter]
	do
		let "linecounter++"
	done < "$filenameinput"

# case scenarios:
# <userinput> ... </userinput> inline definition
# <userinput> spans in more lines
# </userinput> ends spanning
#

	for ((linecounter=0; linecounter < ${#filearray[@]}; linecounter++));
	do
	if [[ "${filearray[linecounter]}" =~ '<userinput>' ]] && \
	   [[ "${filearray[linecounter]}" =~ '</userinput>' ]] ; #scenario 1
	then
			printf "%s\n" "${filearray[linecounter]}"
	elif [[ "${filearray[linecounter]}" =~ '<userinput>' ]] && \
	     [[ ! "${filearray[linecounter]}" =~ '</userinput>' ]] ; #scenario 2
	then
			printf "%s\n" "${filearray[linecounter]}"
			let "collectSTART= linecounter + 1"
	elif [[ ! "${filearray[linecounter]}" =~ '<userinput>' ]] && \
	     [[  "${filearray[linecounter]}" =~ '</userinput>' ]] ; #scenario 3
	then
	let "collectSTOP=linecounter"
	let "linecounter++"
	if [ $((collectSTOP - collectSTART)) -ge 1 ] ;
	then
for ((addthis=$collectSTART; addthis < $((collectSTOP + 1)); addthis++));
	do
		printf "%s\n" "${filearray[addthis]}"
	done
	fi
			printf "%s\n" "${filearray[linecounter]}"
			let "collectSTOP=0"
			let "collectSTART=0"
	fi
	done
	filearray=();
	linecounter=0
	done
	cd ..
done

# ----------------------------------CUT ENDS---------------------




More information about the alfs-discuss mailing list