Unix Shell - Need help to Build logic

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > March 2006 > Need help to Build logic





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Need help to Build logic
pankaj_wolfhunter@yahoo.co.in

2006-03-14, 2:49 am

Greetings,
I have a file like

my_details1
my_name
my_details1
my_address
my_details1
my_number
your_details
your_name
your_details
your_emailid
your_details
your_number
.......
.......
.......

In the file, the first line represents the table name and the
consecutive field represents its column.
like my_details1 is the name of the table and my_name, my_address,
my_number represents its respective fields. and then the second table
starts as your_details with its respective fields.
I want to take this whole file in a loop and for a particular table i
want to put all its respective fields into a new file. as

echo SELECT my_name, my_address, my_number FROM my_details1 >
newfile.sql

I am not able to build a logic for this.

Can someone help me?

Any help would be appreciated.

TIA

Icarus Sparry

2006-03-14, 2:49 am

On Mon, 13 Mar 2006 22:38:30 -0800, pankaj_wolfhunter wrote:

> Greetings,
> I have a file like
>
> my_details1
> my_name
> my_details1
> my_address
> my_details1
> my_number
> your_details
> your_name
> your_details
> your_emailid
> your_details
> your_number
> ......
> ......
> ......
>
> In the file, the first line represents the table name and the
> consecutive field represents its column.
> like my_details1 is the name of the table and my_name, my_address,
> my_number represents its respective fields. and then the second table
> starts as your_details with its respective fields.
> I want to take this whole file in a loop and for a particular table i
> want to put all its respective fields into a new file. as
>
> echo SELECT my_name, my_address, my_number FROM my_details1 >
> newfile.sql
>
> I am not able to build a logic for this.
>
> Can someone help me?
>
> Any help would be appreciated.
>
> TIA


Untested (i.e. I just typed this in).

while read tablename
do
read cname
if [ "X$tablename" = "X$lasttable" ]
then
# This is another entry from the same table, append it
columns="$columns, $cname"
else
[ -n "$lasttable" ] &&
echo "SELECT $columns FROM $lasttable"
lasttable="$tablename"
columns="$cname"
fi
done
# output the final entry if there is one
[ -n "$lasttable" ] &&
echo "SELECT $columns FROM $lasttable"

The basic idea is to have a loop which reads in the tablename, and inside
the loop we read the column. If the table name has changed, and we have
something ready for output then we output it, otherwise we add the column
onto the list of columns we want.

Put this into a file, redirect stdin from your file, send the output to
newfile.sql

pankaj_wolfhunter@yahoo.co.in

2006-03-14, 2:49 am

Thanks a ton Icarus. It works. Thanks again.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com