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

Splitting data into two columns
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

Report this thread to moderator Post Follow-up to this message
Old Post
Joe Farish
11-16-04 11:50 PM


Re: Splitting data into two columns
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

Report this thread to moderator Post Follow-up to this message
Old Post
Jürgen Kahrs
11-16-04 11:50 PM


Re: Splitting data into two columns
In 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(/,/," ")


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
11-16-04 11:50 PM


Re: Splitting data into two columns

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.



Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
11-16-04 11:50 PM


Re: Splitting data into two columns
Ed 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

Report this thread to moderator Post Follow-up to this message
Old Post
Robert Katz
11-16-04 11:50 PM


Re: Splitting data into two columns
In 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'


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
11-16-04 11:50 PM


Re: Splitting data into two columns
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.)



Report this thread to moderator Post Follow-up to this message
Old Post
A Ferenstein
11-17-04 08:55 AM


Re: Splitting data into two columns

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.

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


Re: Splitting data into two columns
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.


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


Re: Splitting data into two columns
> 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.



Report this thread to moderator Post Follow-up to this message
Old Post
A Ferenstein
11-17-04 08:55 AM


Sponsored Links




Last Thread Next Thread Next
Pages (3): [1] 2 3 »
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:09 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.