03-14-06 07: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
[ Post a follow-up to this message ]
|