Home > Archive > AWK > January 2006 > Problem creating csv file
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 |
Problem creating csv file
|
|
|
| I run the following command from a dos batch file to produce a comma
seperated text file:
gawk.exe -f Fund.awk Input.txt >> Output.txt
The Fund.awk program contains the following:
/Global Fund of Funds/{printf ("FEGL,")}
/Price data as at/{printf substr($0, 34, 4) "/"} # Year
/Price data as at/{printf substr($0, 30, 3) "/"} # Month
/Price data as at/{printf substr($0, 27, 2) ","} # Day
/NAV Price/{printf substr ($0, 24, 6)}
The Input.txt file has the following data and format:
Global Fund of Funds
Price data as at: 15-Dec-2005
NAV Price (c): 208.10 c
After the above Input.txt file is processed by Fund.awk the following
output is created in Output.txt:
FEGL,2005/Dec/15,208.10
i.e. a comma seperated file containing 3 "columns".
The Problem:
I want the output in the following format:
FEGL,208.10,2005/Dec/15
(The date is at the end, before the price)
I've tried changing the Fund.awk program to:
/Global Fund of Funds/{printf ("FEGL,")}
/NAV Price/{printf substr ($0, 24, 6)}
/Price data as at/{printf substr($0, 34, 4) "/"} # Year
/Price data as at/{printf substr($0, 30, 3) "/"} # Month
/Price data as at/{printf substr($0, 27, 2) ","} # Day
but this produces the exact same result, ie:
FEGL,2005/Dec/15,208.10
It appears that changing the position of
/NAV Price/{printf substr ($0, 24, 6)}
does not change the output.
How do I get the output to contain the Price BEFORE the Date
ie: FEGL,208.10,2005/Dec/15 ?
Thanks
| |
| Jürgen Kahrs 2006-01-10, 3:58 am |
| Luis wrote:
> I run the following command from a dos batch file to produce a comma
> seperated text file:
>
> gawk.exe -f Fund.awk Input.txt >> Output.txt
I guess this is a follow-up to your earlier posting.
You probably read this newsgroup via Google.
You should have posted this a follow-up with proper
quoting, then the others could understand the context.
> The Problem:
> I want the output in the following format:
>
> FEGL,208.10,2005/Dec/15
> (The date is at the end, before the price)
Put these two lines into Fund.awk and it will do:
/Price data as at/ { split($NF, d, "-") }
/NAV Price/{ printf("FEGL,%s,%s/%s/%s\n", $(NF-1), d[3], d[2],d[1] ) }
At least on my SuSE Linux it worked like this:
gawk -f Fund.awk Input.txt
FEGL,208.10,2005/Dec/15
With your MS-DOS machine, it might be necessary to
make a change to the trailing newline-char.
Now for the not-so-nice news: Read a beginners tutorial about AWK.
For example the one at WikiPedia:
http://en.wikipedia.org/wiki/Awk
Dont be so arrogant to skip the first paragraphs.
From your example scripts it is obvious that you
should read the beginnig of the tutorial and try
to really understand what it means.
| |
| Ed Morton 2006-01-10, 3:58 am |
| Jürgen Kahrs wrote:
> Luis wrote:
>
>
>
>
> I guess this is a follow-up to your earlier posting.
> You probably read this newsgroup via Google.
> You should have posted this a follow-up with proper
> quoting, then the others could understand the context.
>
>
>
>
> Put these two lines into Fund.awk and it will do:
>
> /Price data as at/ { split($NF, d, "-") }
> /NAV Price/{ printf("FEGL,%s,%s/%s/%s\n", $(NF-1), d[3], d[2],d[1] ) }
Alternatively, since it's gawk:
BEGIN{OFS=","; RS="Global.*Funds"}
NF{$5=gensub(/(.*)-(.*)-(.*)/,"\\3/\\2/\\1","",$5); print "FEGL",$9,$5}
Regards,
Ed.
| |
|
| On Tue, 20 Dec 2005 23:55:06 +0100, Jürgen Kahrs
<Juergen.KahrsDELETETHIS@vr-web.de> wrote:
>I guess this is a follow-up to your earlier posting.
I haven't posted to this group in months.
>You probably read this newsgroup via Google.
I read it via Forté Agent 3.2.
>You should have posted this a follow-up with proper
>quoting, then the others could understand the context.
What makes you think I was quoting? This was not a follow-up post. It
was the start of a new thread.
>Put these two lines into Fund.awk and it will do:
>
>/Price data as at/ { split($NF, d, "-") }
>/NAV Price/{ printf("FEGL,%s,%s/%s/%s\n", $(NF-1), d[3], d[2],d[1] ) }
>
>At least on my SuSE Linux it worked like this:
>
>gawk -f Fund.awk Input.txt
>FEGL,208.10,2005/Dec/15
It also works on my Windows XP and Red Hat machines. Thanks.
>With your MS-DOS machine, it might be necessary to
>make a change to the trailing newline-char.
It was not necessary on Windows XP SP2.
| |
| Jürgen Kahrs 2006-01-10, 3:58 am |
| Luis wrote:
>
> What makes you think I was quoting? This was not a follow-up post. It
> was the start of a new thread.
OK, sorry, so I mixed up your posting with someone else's.
Some w s ago we had a similar posting from Jack Smith at JP Morgan:
[color=darkred]
> jack.s.smith@jpmorgan.com wrote:
>
You see .. 15.000 lines of AWK and I thought it was yours.
|
|
|
|
|