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: Splitting data into two columns - comp.lang.awkI 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 50... sed/awk/ etc: Split a long list into tab seperated columns of ...I have a long list of \n seperated entries Name1 Name2 Name3 [snip] Is there a smart way to split it into columns of constant height , say 10 eleme... split a multivalue text fields in columns - comp.groupware.lotus ...Splitting data into two columns - comp.lang.awk OFS, the output field > separatr" is unchanged (i.e. a space ... The real most time consuming process is splitting the text ... Re: Put to split a string into two lines ? - comp.soft-sys.sas ...Splitting data into two columns - comp.lang.awk Re: Put to split a string into two lines ? - comp.soft-sys.sas ..... much messier than splitting it into variables first. Splitting data in Matlab - comp.soft-sys.matlabSplitting data into two columns - comp.lang.awk Splitting data in Matlab - comp.soft-sys.matlab Splitting data into two columns - comp.lang.awk Splitting data in Matlab ... Put to split a string into two lines ? - comp.soft-sys.sas ...Splitting data into two columns - comp.lang.awk Put to split a string into two lines ? - comp.soft-sys.sas ... Splitting data into two columns - comp.lang.awk Re: Put to ... append two columns in one - comp.soft-sys.sasSplitting data into two columns - comp.lang.awk... in output excel files - comp.soft-sys.matlab ... reformatting data from rows to columns..... - comp.soft-sys.sas ... data splitting: train&test or train/test/validate - comp.soft-sys ...Splitting data into two columns - comp.lang.awk Data may be split into "train" and "test" groups via simple random sampling ("SRS ... pattern - comp.lang.awk... with EOD ... Column headers for exported data - comp.databases.filemaker ...The question was how to get field names into Excel: I provided a comparison of ... Export data to a CSV file with field headers in one column and the data in columns 2-6 (each ... Working with data that has multiple columns and rows - comp.lang ...Splitting data into two columns - comp.lang.awk Working with data that has multiple columns and rows - comp.lang ... I would like to know how to first read into this data ... Newbie: group and print by column 1, awk? - comp.unix.shell ...Splitting data into two columns - comp.lang.awk... 1000 0 >> 1500 0 > >{print $1, $2} > >untested Assuming the OP is total newbie, you'd ... equivalent of doing: > > awk ... even space between columns - comp.text.texSplitting data into two columns - comp.lang.awk... 1000,0 1500,0 I would like to extract the data into two columns seperated by a space eg ... that nawk/gawk allows one ... Plot field lines of vector fields using streamslice - comp.soft ...Splitting data into two columns - comp.lang.awk... why there are now spaces between fields - I ... 1=$1, it causes $0 to be reconstructed using the value of OFS as the ... Divide the data set into four groups - comp.soft-sys.sas ...Follow - Computer Group Divide the data set into four groups - comp.soft-sys.sas ... Re: Put to split a string into two lines ? - comp.soft-sys.sas ... Divide the data set ... split at a pattern - comp.lang.awkSplitting data into two columns - comp.lang.awk Data may be split into "train" and "test" groups via simple random sampling ("SRS ... pattern - comp.lang.awk... with EOD ... How to Split Excel Data Into Two Columns | eHow.comPerhaps you had to import or convert data into an Excel spreadsheet, and some pieces of information are now lumped into one column. Having combined data, such as a ... How to Split Data into Multiple Columns in Excel 2010 - For DummiesUse the Convert Text to Columns Wizard in Excel 2010 when you need to split combined data into separate columns, such as a first name and last name; or city, state, and 7/24/2012 10:35:23 PM
|