field separator 
Author Message
 field separator

Any way to separate a text record into individual characters? Like
record "abcdef" would be $1=a $2=b $3=c ...I tride setting FS to "" but
no luck.

Thanks Don



Mon, 08 Apr 2002 03:00:00 GMT  
 field separator
probably the best way to do this is with a short function to extract
characters to an array, but you may be just as well off to access the
characters individually since they are already in the array in the text
record.

Maybe I'm misunderstanding something in your request.

Sam


Quote:
> Any way to separate a text record into individual characters? Like
> record "abcdef" would be $1=a $2=b $3=c ...I tride setting FS to "" but
> no luck.

> Thanks Don



Mon, 08 Apr 2002 03:00:00 GMT  
 field separator


Quote:
>Any way to separate a text record into individual characters? Like
>record "abcdef" would be $1=a $2=b $3=c ...I tride setting FS to "" but
>no luck.

>Thanks Don

Gawk lets you set the fieldwidths with the FIELDWIDTHS constant.

Something like this would do it:

echo "abcde" |
gawk 'BEGIN{FIELDWIDTHS="1 1 1 1 1 1 1 1 1 1 1 1 1 1 1"}
      {print $1, $2, $3, $4, $5, $6}'

You could also assign things using the substr function.
Something like this:

awk '{a=$0; c=length ; for(i=1;i<=c;i++){$i=substr(a,i,1)};
      print $1, $2, $3, $4, $5, $6}'

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 08 Apr 2002 03:00:00 GMT  
 field separator

Quote:

>probably the best way to do this is with a short function to extract
>characters to an array, but you may be just as well off to access the
>characters individually since they are already in the array in the text
>record.

That function already exists, it's called split, and it's part of
most versions (not the old one however) of awk/gawk.

Chuck Demas
Needham, Mass.

Quote:

>Maybe I'm misunderstanding something in your request.

>Sam



>> Any way to separate a text record into individual characters? Like
>> record "abcdef" would be $1=a $2=b $3=c ...I tride setting FS to "" but
>> no luck.

>> Thanks Don

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Tue, 09 Apr 2002 03:00:00 GMT  
 field separator

Quote:


> > Any way to separate a text record into individual characters? Like
> > record "abcdef" would be $1=a $2=b $3=c ...I tride setting FS to ""
> > but no luck.

> Gawk lets you set the fieldwidths with the FIELDWIDTHS constant.

> Something like this would do it:

> echo "abcde" |
> gawk 'BEGIN{FIELDWIDTHS="1 1 1 1 1 1 1 1 1 1 1 1 1 1 1"}
>       {print $1, $2, $3, $4, $5, $6}'

What?!

From the gawk manual:

     If FS is the null string, then each individual character
     becomes a separate field.

Here's a demonstration:

$ echo foobar | gawk '{ print $1, $2, $3, $4, $5, $6 }' FS=''
f o o b a r
$

In his inquiry, Donnie Roberts stated that he had tried setting FS
to the null string, but that it didn't work. (See quoted text
above.) The answer to his question may simply be: "Use gawk."

Donnie: Are you sure you really want to process text a character
at a time using awk? Have you explored the possibility of using
regular expression pattern matching to solve your problem? Or
writing the program in a different language--C, perhaps? Just a
thought...

--
Jim Monty

Tempe, Arizona USA



Tue, 09 Apr 2002 03:00:00 GMT  
 field separator


Quote:


>> > Any way to separate a text record into individual characters? Like
>> > record "abcdef" would be $1=a $2=b $3=c ...I tride setting FS to ""
>> > but no luck.

>> Gawk lets you set the fieldwidths with the FIELDWIDTHS constant.

>> Something like this would do it:

>> echo "abcde" |
>> gawk 'BEGIN{FIELDWIDTHS="1 1 1 1 1 1 1 1 1 1 1 1 1 1 1"}
>>       {print $1, $2, $3, $4, $5, $6}'

>What?!

>From the gawk manual:

>     If FS is the null string, then each individual character
>     becomes a separate field.

I think I need some sleep.  I'm obviously not thinking clearly.

Chuck Demas
Needham, Mass.

- Show quoted text -

Quote:

>Here's a demonstration:

>$ echo foobar | gawk '{ print $1, $2, $3, $4, $5, $6 }' FS=''
>f o o b a r
>$

>In his inquiry, Donnie Roberts stated that he had tried setting FS
>to the null string, but that it didn't work. (See quoted text
>above.) The answer to his question may simply be: "Use gawk."

>Donnie: Are you sure you really want to process text a character
>at a time using awk? Have you explored the possibility of using
>regular expression pattern matching to solve your problem? Or
>writing the program in a different language--C, perhaps? Just a
>thought...

>--
>Jim Monty

>Tempe, Arizona USA

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Tue, 09 Apr 2002 03:00:00 GMT  
 field separator

writes:

<snip>

Quote:
>     If FS is the null string, then each individual character
>     becomes a separate field.

>Here's a demonstration:

>$ echo foobar | gawk '{ print $1, $2, $3, $4, $5, $6 }' FS=''
>f o o b a r
>$

<snip>

For a hoot, I checked B W Kernighan's awk95 (merely a Win32 port of his source
for the folks who like WinTel bashing). My test script was a little different,
and it gave a surprising result:

D:\> echo abcdefg | awk95 "BEGIN{FS=x}{while(NF--)print $NF}"
g
f
e
d
c
b
a
abcdefg

while gawk gave

echo abcdefg | gawk "BEGIN{FS=x}{while(NF--)print $NF}"
g
f
e
d
c
b
a

(There's a zero-length last line below 'a'.)

Further testing showed that in gawk, decrementing NF eliminated fields right to
left from $0. However, BWK's one true awk leaves $0 as-is while NF is
manipulated.

Which is 'right' ?



Wed, 10 Apr 2002 03:00:00 GMT  
 field separator

   >From the gawk manual:
   >If FS is the null string, then each individual character
   >becomes a separate field.
   >Here's a demonstration:
   >$ echo foobar | gawk '{ print $1, $2, $3, $4, $5, $6 }' FS=''
   >f o o b a r
   >$
If you don't have gawk, you can use substr($0,x,1) instead of $x.

Net-Tamer V 1.08X - Test Drive



Wed, 10 Apr 2002 03:00:00 GMT  
 field separator

Quote:


> > From the gawk manual:
> >     If FS is the null string, then each individual character
> >     becomes a separate field.
> > Here's a demonstration:
> > $ echo foobar | gawk '{ print $1, $2, $3, $4, $5, $6 }' FS=''
> > f o o b a r
> > $

> If you don't have gawk, you can use substr($0,x,1) instead of $x.

And if you don't have a computer, you can chisel foobar in stone.

--
Jim Monty

Tempe, Arizona USA



Wed, 10 Apr 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Dealing with commas as a field separator AND possibly within a field

2. Hexadecimal Field Separator

3. Multiple field separators for paranthesis not working

4. multiple field separators in AWK

5. Field Separator

6. Field Separator larger than one character

7. Field Separator not working

8. Quoted field separators

9. backslash as field separator

10. Problem with Field Separators

11. AWK with 2 field separators

12. Strange AWK behaviour with Field-Separators

 

 
Powered by phpBB® Forum Software