Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting data in an array

Status
Not open for further replies.

gringomike

Technical User
Aug 6, 2003
148
GB
Hi all,

I've written a korn shell script, part of which populates an array with the contents of a text file.

The text is inserted into the first position of the array in this format (which is how I want it to look when it is written back out);

<position 1>
line1
line2
line3
line4

<positions 2>
line1
line2
line3
line4
line5

The problem I have is that it comes back out in this format;

line1 line2 line3 line4
line1 line2 line3 line4 line5


This section generates the entry in the array;

NEW=`cat $line | sed -e 's/^some_text=//' -e 's/^moretext="//' -e 's/"//' -e '/^$/d' -e 's/^[ \]*//
' -e '1d' -e '$d'`

baseline=$NEW

# Set up arrays with environment name and log path

envir[arrayCount]="$tempEnvir"
app[arrayCount]="$tempApp"
files[((arrayCount))]="$line"
base[arrayCount]="$baseline"
let arrayCount="arrayCount+1"


This section outputs the contents of the array into an HTML file;

for ENV1 in $tempEnvir
do
echo "<td valign="top">${envir[count]}</td>">>$PROPS_LOG
echo "<td valign="top">${app[count]}</td>">>$PROPS_LOG
echo "<td valign="top">${base[count]}</td>">>$PROPS_LOG
echo "<td>">>$PROPS_LOG
done


My initial thought was that I would need to nest another "for" loop inside the section of code that generates the HTML file however everything I've tried so far has been unsuccessful.

Does anybody know of a way to do this?

I hope this makes sense!

Thanks

GM
 
You may try to replace this:
baseline=$NEW
By something like this:
baseline=`echo "$NEW" | awk '{x=$0;gsub("\n","<BR>",x);print x}'`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks for the reply PHV but this doesn't make any difference to the final output. The data still comes out like this;

line1 line2 line3 line4
line1 line2 line3 line4 line5

rather than this;

line1
line2
line3
line4

line1
line2
line3
line4
line5

Any other ideas?

Thanks

GM
 
Sorry, you have to also replace this:
NEW=`cat $line
By this:
NEW=`cat "$line"

And how is the line variable populated ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
No joy with this either I'm afraid.

$line is populated by doing a cat on a text file which is populated with a list of files. This list is generated by running a find command in a prior "for loop".

for line in `cat list2`
do
<perform various operations>


if I run the command 'cat list2' I get the following output;

/path/to/file/file1.txt
/path/of/file/file2.txt


Is this of any use?

Thanks

GM
 
And this ?
base[arrayCount]=`echo "$NEW" | awk '{x=$0;gsub(" ","<BR>",x);print x}'`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Still no luck I'm afraid.

This is the whole function. Perhaps there's something else contributing to the problem? The changes you suggested I make to the script result in the HTML file becoming garbled.


function readLine
{
arrayCount=0
> ./list

# for loop to go through and find file1.txt
# within the web directory.

for line in `find /path -name "file1.txt" | sort `
do
echo $line >> ./list
done

sort ./list > ./list2

mv a.txt a.txt.old

for line in `cat ./list2`
do
ls -1 $line | awk -F/ '{print $3}' > /path/to/file/a.txt
cat $line >> /path/to/file/a.txt


cat a.txt | sed -e 's/^some_text=//' -e 's/^moretext="//' -e 's/"//' -e
'/^$/d' -e 's/^[ \]*//' |awk -F/ -f search.awk > z.txt

for i in `cat z.txt`
do
echo $i |sed -e '$s!.$!!' -e '/^$/d' >> y.txt
done

REGION=`cat y.txt |awk -F, '{print $NR}'`
DEPFAM=`cat y.txt |awk -F, '{print $NF}'`
NEW=`cat "$line" | sed -e 's/^some_text=//' -e 's/^moretext="//' -e 's/
"//' -e '/^$/d' -e 's/^[ \]*//' -e '1d' -e '$d'`

tempEnvir=$REGION
tempApp=$DEPFAM
# baseline=$NEW
# baseline=`echo "$NEW" | awk '{x=$0;gsub("\n","<BR>",x);print x}'`
# baseline=`echo $NEW | awk '{x=$0;gsub("\n","<BR>",x);print x}'`

# Set up arrays with environment name and log path

envir[arrayCount]="$tempEnvir"
app[arrayCount]="$tempApp"
files[((arrayCount))]="$line"
base[arrayCount]=`echo "$NEW" | awk '{x=$0;gsub(" ","<BR>",x);print x}'`
let arrayCount="arrayCount+1"
done
}


Could it be that the way to solve this would be by performing some operation on the line highlighted in bold in my first posting?

Would something like your suggested "awk" command work if it was carried out as the data is written back out of the array rather than as it is written in?

Thanks for your help!

GM
 
Hi PHV,

I've managed to work out a solution.

I changed this line;

NEW=`cat "$line" | sed -e 's/^deployment_family=//' -e 's/^baselines="//' -e 's/
"//' -e '/^$/d' -e 's/^[ \]*//' -e '1d' -e '$d'`

to this;

NEW=`cat "$line" | sed -e 's/^deployment_family=//' -e 's/^baselines="//' -e 's/
"//' -e '/^$/d' -e 's/^[ \]*//' -e '1d' -e '$d' -e 's/$/<br>/'`

Appending the extra <br> statement at the end each line in $line sorted the problem.

Thanks alot for your help!

GM
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top