Code Comments
Programming Forum and web based access to our favorite programming groups.I have some data in the following format: 0,0 500,0 1000,0 1500,0 I would like to extract the data into two columns seperated by a space eg: 0 0 500 0 1000 0 1500 0 Thanks in advance, JAF
Post Follow-up to this messageJoe Farish wrote:
> I have some data in the following format:
>
> 0,0
> 500,0
> 1000,0
> 1500,0
>
> I would like to extract the data into two columns seperated by a space eg:
>
> 0 0
> 500 0
> 1000 0
> 1500 0
{print $1, $2}
untested
Post Follow-up to this messageIn article <2vrlkeF2pfu91U1@uni-berlin.de>,
examnotes <Juergen.KahrsDELETETHIS@vr-web.de> wrote:
>Joe Farish wrote:
>
>{print $1, $2}
>
>untested
Assuming the OP is total newbie, you'd have to add in the setting of FS=","
in order to make that solution complete.
If the problem really is as simple as it seems, then it would be easier to
use substitution. I.e.: gsub(/,/," ")
Post Follow-up to this message
Kenny McCormack wrote:
> In article <2vrlkeF2pfu91U1@uni-berlin.de>,
> examnotes <Juergen.KahrsDELETETHIS@vr-web.de> wrote
:
>
>
>
> Assuming the OP is total newbie, you'd have to add in the setting of FS=",
"
> in order to make that solution complete.
>
> If the problem really is as simple as it seems, then it would be easier to
> use substitution. I.e.: gsub(/,/," ")
No need for gsub:
awk -F, '$1=$1'
or, if the OP is worried about retaining blank lines:
awk -F, '{$1=$1}1'
Regards,
Ed.
Post Follow-up to this messageEd Morton wrote:
>
>
[ . . . ]
[ . . . ]
>
> awk -F, '$1=$1'
>
> or, if the OP is worried about retaining blank lines:
Not just a blank line, but if $1 == 0 or $1 == "", nothing will print
>
> awk -F, '{$1=$1}1'
--
Regards,
---Robert
Post Follow-up to this messageIn article <SKqmd.2980$WC1.1106@news.cpqcorp.net>,
Robert Katz <katz@hp.com> wrote:
>Ed Morton wrote:
>
>[ . . . ]
>
>
>[ . . . ]
>
>
>
>Not just a blank line, but if $1 == 0 or $1 == "", nothing will print
>
>
> awk -F, '{$1=$1}1'
awk -F, '($1=$1)1'
Post Follow-up to this messageEd,
> awk -F, '$1=$1'
can you please explain this syntax?
From gawk man, "Assigning a value to an existing field causes the whole
record to be rebuilt when $0 is referenced." (However, I don't
understand why there are now spaces between fields - I would have thought,
gawk would have inserted "," between the old fields.) I'm coming from
awk/nawk where this syntax is illegal.
> awk -F, '{$1=$1}1'
Can you please explain the last 1? (Again, this syntax is illegal in
awk/nawk.)
Post Follow-up to this message
A Ferenstein wrote:
> Ed,
>
>
>
> can you please explain this syntax?
> From gawk man, "Assigning a value to an existing field causes the who
le
> record to be rebuilt when $0 is referenced." (However, I don't
> understand why there are now spaces between fields - I would have thought,
> gawk would have inserted "," between the old fields.) I'm coming from
> awk/nawk where this syntax is illegal.
-F sets "FS" which is the input field separator. OFS, the output field
separatr" is unchanged (i.e. a space in this case). By assigning $1=$1,
it causes $0 to be reconstructed using the value of OFS as the field
separator when the default action (i.e. print) takes effect as a result
of the "$1=$1" "condition" evaluating to $1, which is neither zero nor
null in most cases, thus it's a true condition and thus it initiates the
default action, a print of $0. This just falls flat if $1 is a null
string or zero.
>
>
> Can you please explain the last 1? (Again, this syntax is illegal in
> awk/nawk.)
>
The "{$1=$1}" causes $0 to be reconstructed when $0 is referenced.
Putting the "1" there is explicitly stating a true condition. It's
exactly the equivalent of doing:
awk -F, '{$1=$1}{print $0}'
I was just being concise (aka lazy).
Hope that clears it up.
Ed.
Post Follow-up to this messageIn article <irmdnWSwCdjGIwfcRVn-rA@comcast.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
...
>The "{$1=$1}" causes $0 to be reconstructed when $0 is referenced.
>Putting the "1" there is explicitly stating a true condition. It's
>exactly the equivalent of doing:
>
>awk -F, '{$1=$1}{print $0}'
>
>I was just being concise (aka lazy).
heh heh...
Note that the only bit of the syntax that Solaris://usr/bin/awk doesn't
like is using a simple number (i.e., 1) as a "pattern". You can change
that to "1==1" to make it work with Solaris://usr/bin/awk, or, more
sensibly, just use any other awk implementation. I think the OP is
mistaken in saying "awk/nawk", since Solaris://usr/bin/nawk does support
the "1" syntax.
Post Follow-up to this message> I think the OP is mistaken in saying "awk/nawk", since Solaris://usr/bin/nawk does support the "1" syntax. Yes, I'm incorrect. Unlike Awk, Nawk does support it.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.