Splitting data into two columns

  • Follow


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
0
Reply jaf893 11/15/2004 12:19:55 PM

Joe 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
0
Reply ISO 11/15/2004 12:26:54 PM


In article <2vrlkeF2pfu91U1@uni-berlin.de>,
=?ISO-8859-1?Q?J=FCrgen_Kahrs?=  <Juergen.KahrsDELETETHIS@vr-web.de> wrote:
>Joe 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

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(/,/," ")

0
Reply gazelle 11/15/2004 12:34:02 PM


Kenny McCormack wrote:

> In article <2vrlkeF2pfu91U1@uni-berlin.de>,
> =?ISO-8859-1?Q?J=FCrgen_Kahrs?=  <Juergen.KahrsDELETETHIS@vr-web.de> wrote:
> 
>>Joe 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
> 
> 
> 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.


0
Reply Ed 11/15/2004 2:54:12 PM

Ed Morton wrote:
> 
> 

[ . . . ]

>>> Joe 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

[ . . . ]


> 
> 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
0
Reply Robert 11/16/2004 5:26:10 PM

In article <SKqmd.2980$WC1.1106@news.cpqcorp.net>,
Robert Katz  <katz@hp.com> wrote:
>Ed Morton wrote:
>> 
>> 
>
>[ . . . ]
>
>>>> Joe 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
>
>[ . . . ]
>
>
>> 
>> 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'

awk -F, '($1=$1)1'

0
Reply gazelle 11/16/2004 5:41:58 PM

Ed,

> 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.)


0
Reply A 11/17/2004 2:11:40 AM


A Ferenstein wrote:

> Ed,
> 
> 
>>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.

-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.


> 
>>awk -F, '{$1=$1}1'
> 
> 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.
0
Reply Ed 11/17/2004 2:58:32 AM

In 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.

0
Reply gazelle 11/17/2004 3:20:46 AM

> 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.


0
Reply A 11/17/2004 5:03:53 AM

Ed,
thanks for clarifying... I have done a lot of awk programming but I've never
ever used OF/OFS.
Your '{$1=$1}1' is tricky syntax and I wonder if you realise how confusing
it may be to those who don't try to reduce number of lines in their
solutions.
If you wrote:
 {$1=$1}
1
it would have been much clearer. It's even a pity that nawk/gawk allows one
to write such ugly code - without even a space between } and 1.
I'd be really interested in looking at some of your larger awk programs if
you care to share (via email). In return, I will show you mine if you care.
Alex

"Ed Morton" <morton@lsupcaemnt.com> wrote in message
news:irmdnWSwCdjGIwfcRVn-rA@comcast.com...
>
>
> A Ferenstein wrote:
>
> > Ed,
> >
> >
> >>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.
>
> -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.
>
>
> >
> >>awk -F, '{$1=$1}1'
> >
> > 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.


0
Reply A 11/17/2004 7:48:10 AM

A Ferenstein wrote:
> Ed,
> thanks for clarifying... I have done a lot of awk programming but I've never
> ever used OF/OFS.
> Your '{$1=$1}1' is tricky syntax and I wonder if you realise how confusing
> it may be to those who don't try to reduce number of lines in their
> solutions.
> If you wrote:
>  {$1=$1}


> 1
> it would have been much clearer. It's even a pity that nawk/gawk allows one
> to write such ugly code - without even a space between } and 1.

Well, yes and no. The ugly style (largely borrowed from C's syntax, c.f. 
www.ioccc.org :-) _can_ make programs hard to read and maintain. On the 
other hand, it is extermely useful for disposable one line programs 
written inline in the shell.

-Ed

> I'd be really interested in looking at some of your larger awk programs if
> you care to share (via email). In return, I will show you mine if you care.
> Alex




-- 
(You can't go wrong with psycho-rats.)       (er258)(@)(eng.cam)(.ac.uk)

/d{def}def/f{/Times findfont s scalefont setfont}d/s{10}d/r{roll}d f 5/m
{moveto}d -1 r 230 350 m 0 1 179{1 index show 88 rotate 4 mul 0 rmoveto}
for /s 15 d f pop 240 420 m 0 1 3 { 4 2 1 r sub -1 r show } for showpage

0
Reply E 11/17/2004 10:05:22 AM


A Ferenstein wrote:
> Ed,
> thanks for clarifying... I have done a lot of awk programming but I've never
> ever used OF/OFS.
> Your '{$1=$1}1' is tricky syntax and I wonder if you realise how confusing
> it may be to those who don't try to reduce number of lines in their
> solutions.
> If you wrote:
>  {$1=$1}
> 1
> it would have been much clearer.

I'm all for clarity in software. In fact when I'm teaching software 
engineering I talk about categorising software using 4 Cs like you do 
for diamonds, but instead of cut, color, carat, and clarity, you 
consider cohesion, coupling, consistency, and clarity. But what's the 
point for an 8-character program? It's only tricky the first time you 
see it. Bet you'll never have a problem understanding it again.

  It's even a pity that nawk/gawk allows one
> to write such ugly code - without even a space between } and 1.

It's useful for at-the-prompt one-liners like this.

> I'd be really interested in looking at some of your larger awk programs if
> you care to share (via email). In return, I will show you mine if you care.
> Alex

I don't tend to write large awk programs (I use C and VFSM), but when I 
do they're shining examples of the art ;-). Actually, I've probably only 
written one or 2 large awk scripts and I hadn't discovered gawk when I 
wrote them (so I had to fight with old awk) and I've learned so much 
about awk from participating in this NGS that I'd be embarassed to let 
anyone see them. If you want to contact me by email though, just take 
the "spam" out of my posted email address.

	Ed.
0
Reply Ed 11/17/2004 1:15:09 PM

> 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.

Brian Kernighan's Awk ("The One True Awk") handles both of the following:

awk -F, "$1=$1"

awk -F, "{$1=$1}1"

Tested  with awk95.exe.
0
Reply w_a_x_man 11/19/2004 5:31:49 AM

13 Replies
239 Views

(page loaded in 1.512 seconds)

Similiar Articles:


















7/24/2012 10:35:23 PM


Reply: