Home > Archive > AWK > November 2004 > Splitting data into two columns
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 |
Splitting data into two columns
|
|
| Joe Farish 2004-11-16, 6:50 pm |
| 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
| |
| Jürgen Kahrs 2004-11-16, 6:50 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
| |
| Kenny McCormack 2004-11-16, 6:50 pm |
| In article <2vrlkeF2pfu91U1@uni-berlin.de>,
=?ISO-8859-1?Q?J=FCrgen_Kahrs?= <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(/,/," ")
| |
| Ed Morton 2004-11-16, 6:50 pm |
|
Kenny McCormack wrote:
> In article <2vrlkeF2pfu91U1@uni-berlin.de>,
> =?ISO-8859-1?Q?J=FCrgen_Kahrs?= <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.
| |
| Robert Katz 2004-11-16, 6:50 pm |
| Ed Morton wrote:
>
>
[ . . . ]
[ . . . ]
[color=darkred]
>
> 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
| |
| Kenny McCormack 2004-11-16, 6:50 pm |
| 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'
| |
| A Ferenstein 2004-11-17, 3:55 am |
| 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.)
| |
| Ed Morton 2004-11-17, 3:55 am |
|
A Ferenstein wrote:
> Ed,
>
>
>
> 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.
>
>
> 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.
| |
| Kenny McCormack 2004-11-17, 3:55 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.
| |
| A Ferenstein 2004-11-17, 3:55 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.
| |
| A Ferenstein 2004-11-17, 8:55 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:
>
whole[color=darkred]
thought,[color=darkred]
>
> -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.
>
>
>
> 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.
| |
| E. Rosten 2004-11-17, 8:55 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
| |
| Ed Morton 2004-11-17, 3:56 pm |
|
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.
| |
| Ed Morton 2004-11-18, 8:56 pm |
|
Kenny McCormack wrote:
> In article <2vrlkeF2pfu91U1@uni-berlin.de>,
> =?ISO-8859-1?Q?J=FCrgen_Kahrs?= <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.
| |
| Jürgen Kahrs 2004-11-19, 3:55 am |
| 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
| |
| William James 2004-11-19, 3:55 am |
| > 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.
| |
| Ed Morton 2004-11-19, 8:55 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.
| |
| A Ferenstein 2004-11-21, 3:57 am |
| 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.)
| |
| A Ferenstein 2004-11-21, 3:57 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:
>
whole[color=darkred]
thought,[color=darkred]
>
> -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.
>
>
>
> 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.
| |
| E. Rosten 2004-11-21, 3:57 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
| |
| Ed Morton 2004-11-21, 3:57 am |
|
Kenny McCormack wrote:
> In article <2vrlkeF2pfu91U1@uni-berlin.de>,
> =?ISO-8859-1?Q?J=FCrgen_Kahrs?= <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.
|
|
|
|
|