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 Script Output

Status
Not open for further replies.

gringomike

Technical User
Aug 6, 2003
148
GB
Hi all,

I have written a simple script which generates a text file in the following format;

name1
stuff1="blah-blah-blah
some_text
more_text
even_more
etc_etc"
stuff2="one_line
name2
stuff1="blah-blah-blah
some_text
more_text
even_more
etc_etc"
stuff2=one_line
name3
stuff1="blah-blah-blah
some_text
more_text
even_more
etc_etc"
stuff2=one_line

etc etc.....


I need the file to be in the following format;


NAME STUFF2 STUFF1
name1 one_line blah-blah-blah
some_text
more_text
even_more
etc_etc

name2 one_line blah-blah-blah
some_text
more_text
even_more
etc_etc

name3 one_line blah-blah-blah
some_text
more_text
even_more
etc_etc

I need to format this data (using HTML) into a table that can be uploaded onto our intranet.

I've had a look at Thread822-531443 but this doesn't give me the solution to my problem.

I'm aware that the solution is likely to be found using awk or sed however nothing I've tried so far has worked.

Does anybody know of a way to get the data into the necessary format?

My script currently reads as follows;

find /dir1 -name textfile > /path/to/file/find_output.txt

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


Thanks for your help in advance!

GM

 
Hi all,

I've managed to make some progress.

My data still needs to be in the format listed above but I have managed to get the file I'm working on into csv format with the help of a previous posting from PHV and vgersh99.

My script is now as follows;

#!/bin/ksh

find /dir1 -name textfile > /path/to/file/find_output.txt

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

cat a.txt | sed -e 's/^stuff2=//'
-e 's/^stuff1="//' -e 's/"//' -e
'/^$/d' -e 's/^[ \]*//' |awk -f search.awk > x.txt

search.awk has the following code;

BEGIN {
OFS=","
}
{
for(i=1; i <= NF; i++)
if (match($i, /.*[^0-9].*/) && !match($i, /^ *$/))
printf("%s%s", $i, OFS)
}

The file x.txt has the following contents;

name1,line_1,line_2,line_3,line_4,one_line,name2,line_1,line_2,line_3,line_4,line_5,line_6,one_line,name3,line1,line2,......

I need the data in the following format;

NAME STUFF2 STUFF1
name1 one_line line_1
line_2
line_3
line_4

name2 one_line line_1
line_2
line_3
line_4
line_5


Does anybody know a good way to do this?

Thanks!

GM




 
Even better news!

I now have the contents of the file in HTML format but its all on one row.

I added this into my script;

{
echo "<BODY BGCOLOR=#000000 TEXT=#FFFFFF LINK=#FFFFFF \ ALINK=#FFFFFF VLINK=#FF
FFFF TOPMARGIN=0 LEFTMARGIN=0 \
RIGHTMARGIN=0 BOTTOMMARGIN=0 >"
echo "<TABLE WIDTH=90% ALIGN=center BGCOLOR=#000093 BORDER=3 CELLPADDING=1 CELL
SPACING=1><TR><TD>"
echo "<PRE>"
echo "<table border="1">"
echo "<table frame="void">" # no borders
cat x.txt | awk -F, -f table.awk # create individual rows w/ awk
echo "</PRE></TD></TR></TABLE>"
} > y.htm


table.awk contains the following;

{ print "<tr>"
print "<td>"$1"</td>"
print "<td>"$2"</td>"
print "<td>"$3"</td>"
print "<td>"$4"</td>"
print "<td>"$5"</td>"
print "<td>"$6"</td>"
print "<td>"$7"</td>"
print "<td>"$8"</td>"
print "<td>"$9"</td>"
print "<td>"$10"</td>"
print "<td>"$11"</td>"
print "<td>"$12"</td>"
print "<td>"$13"</td>"
print "<td>"$14"</td>"
print "<td>"$15"</td>"
print "</tr>"
}


I'm still not where I want to be though.

Does anybody have any ideas?

Thanks

GM
 
The final version!

Hopefully this will be of some use to others trying to do a similar thing!


#!/bin/ksh
# This script runs a find to get all the current file1.txt files in
# then generates html so that the information can be
# displayed in a browser.

USAGE="usage: $0 arg1 arg2"

#### Variables

server=$1
LOGFILE=$2/file.html
TEMP_PROPS_LOG_2=$2/x.txt

#### functions

# readLine function reads lines one at a time and
# places values into an array.

function readLine
{
arrayCount=0
> ./linelist

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

for line in `find /dir -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" | sed 's/sometext="/sometext="\
/' >> /path/to/file/a.txt
#/' >> /path/to/file/a.txt
# cat $line >> /path/to/file/a.txt
cat a.txt | sed -e 's/^more_text=//' -e 's/^sometext="//' -e 's/"//' -e 's/[ \t]*$//' -e '/^$/d' -e 's/^[ \]*//' |awk -F/ -f search.awk > z.txt

touch y.txt
mv y.txt y.txt.old
touch y.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/^more_text=//' -e 's/^sometext="//' -e 's/"//' -e '/^$/d' -e 's/^[ \]*//' -e '1d' -e '$d' -e 's/$/<br>/'`

tempEnvir=$REGION
tempApp=$DEPFAM
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"
done
}

# generateHTML function generates an html page by pulling in hardcoded html for
# most of the page

function generateHTML
{
# Retrieve the top of the HTML before inserting table
# with the log data.

# Create table
echo "<b>Server: $server Last Generated:`date`</b>">$LOGFILE
echo "<table width=\"100%\" border=\"1\" align=\"left\">">>$LOGFILE
echo "<tr>">>$LOGFILE
echo "<td ><b>Col 1</b></td>">>$LOGFILE
echo "<td ><b>Col 2</b></td>">>$LOGFILE
echo "<td ><b>Col 3</b></td>">>$LOGFILE
echo "</tr>">>$LOGFILE

# while loop to go through files and extract release and version

count=0

while (( $count < ${#files[*]} ))
do
echo "<tr>">>$LOGFILE

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

perl -wpe 's/$/\<br>/m' $TEMP_PROPS_LOG_2 >> $LOGFILE

echo "</td>">>$LOGFILE
echo "</tr>">>$LOGFILE
let count="count + 1"
done
done
echo "</table>">>$LOGFILE
}

if (($# != 2)) then
echo "$USAGE"
echo "arg1 - Server name"
echo "arg2 - DIRECTORY path"

else

#### Run functions
readLine
generateHTML
fi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top