Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Convert Columns to Rows
I have a very large csv file from a spreadsheet.  The first 2 columns are a
description, and the next 52 columns are a value for a w of the year
(using only 3 ws in this example)

Is there a way in Awk to transform these into a format that is loadable into
a relational database such as Oracle?

For example, I'd like to change the following into the latter format.  The
first column would be a derived w number.  That first column is something
I could live without if I had to.

Any help with a simple awk would be appreciated.



washer,sunday,33,44,55
dryer,monday,667,333,44
refrig,monday,555,876,99



1    washer sunday 33
2    washer sunday 44
3    washer sunday 55
1    dryer monday 667
2    dryer monday 333
3    dryer monday 444
1    refrig monday 555
2    refrig monday 876
3    refrig monday 99




Report this thread to moderator Post Follow-up to this message
Old Post
Buck Turgidson
11-17-04 08:56 PM


Re: Convert Columns to Rows
In article <bebs62-3ff.ln1@turf.turgidson.com>,
Buck Turgidson <jc_va@hotmail.com> wrote:
...
Convert:
>washer,sunday,33,44,55
>dryer,monday,667,333,44
>refrig,monday,555,876,99

To:

>1    washer sunday 33
>2    washer sunday 44
>3    washer sunday 55
>1    dryer monday 667
>2    dryer monday 333
>3    dryer monday 444
>1    refrig monday 555
>2    refrig monday 876
>3    refrig monday 99

BEGIN {FS=","}
{
for (i=3; i<=NF; i++)
print i-2,$1,$2,$i
}


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
11-17-04 08:56 PM


Re: Convert Columns to Rows

Buck Turgidson wrote:
> I have a very large csv file from a spreadsheet.  The first 2 columns are 
a
> description, and the next 52 columns are a value for a w of the year
> (using only 3 ws in this example)
>
> Is there a way in Awk to transform these into a format that is loadable in
to
> a relational database such as Oracle?

Sure:

gawk -F, '{for (i=3;i<=NF;i++) print i-2, $1, $2, $i}'

Ed.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
11-17-04 08:56 PM


Re: Convert Columns to Rows
>
> BEGIN {FS=","}
> {
> for (i=3; i<=NF; i++)
>     print i-2,$1,$2,$i
> }
>

Just beautiful.....Thanks to both.



Report this thread to moderator Post Follow-up to this message
Old Post
Buck Turgidson
11-17-04 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:01 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.