Re: awk - single character field separator (341 Views)
Reply
Advisor
Mark Tunnell
Posts: 33
Registered: ‎01-19-2003
Message 1 of 15 (341 Views)
Accepted Solution

awk - single character field separator

How do I assign a field separator of any single character in awk? The following works fine (prints an 'h') in Linux but not in HP-UX:
echo hello | awk 'BEGIN { FS = "" } ; {print $1}
Please use plain text.
Respected Contributor
Paul Cross_1
Posts: 288
Registered: ‎03-18-2002
Message 2 of 15 (341 Views)

Re: awk - single character field separator

hpux awk is not gnu, and therefore has different options and features.

From the gnu website:

Traditionally, the behavior of FS equal to "" was not defined. In this case, most versions of Unix awk simply treat the entire record as only having one field. (d.c.)

-P
Please use plain text.
Advisor
Mark Tunnell
Posts: 33
Registered: ‎01-19-2003
Message 3 of 15 (341 Views)

Re: awk - single character field separator

Thanks for responding. I gave the Linux example so it would be clear what I was trying to do. Do you know what the corresponding HP-UX syntax would be?
Please use plain text.
Honored Contributor
Robert-Jan Goossens
Posts: 7,384
Registered: ‎04-04-2000
Message 4 of 15 (341 Views)

Re: awk - single character field separator

None.

/root# echo hello | awk 'BEGIN { FS="" } ; {print $1}'
hello

download and install gawk + libiconv from
http://hpux.connect.org.uk/hppd/hpux/Gnu/gawk-3.1.4/

Regards,
Robert-Jan
Please use plain text.
Honored Contributor
Stephen Keane
Posts: 1,155
Registered: ‎11-24-2004
Message 5 of 15 (341 Views)

Re: awk - single character field separator

I don't think you can in hpux.

In Linux you could also do

echo hello | awk -F "" '{print $1}'

Please use plain text.
Regular Advisor
S.Rider
Posts: 216
Registered: ‎02-02-2004
Message 6 of 15 (341 Views)

Re: awk - single character field separator

awk -F":"
Ride Boldly Ride, but watch out for El Dorado's
Please use plain text.
Honored Contributor
Stephen Keane
Posts: 1,155
Registered: ‎11-24-2004
Message 7 of 15 (341 Views)

Re: awk - single character field separator

Or,

echo hello | awk 'BEGIN {FIELDWIDTHS=1} { print $1 }'

But sadly, this doesn't help with your problem, as the output on HP is 'hello'.
Please use plain text.
Honored Contributor
Rick Garland
Posts: 4,470
Registered: ‎06-25-1997
Message 8 of 15 (341 Views)

Re: awk - single character field separator

Are other options available?

Could you use the substr() function within perl or shell?

What about sed?
Please use plain text.
Respected Contributor
Paul Cross_1
Posts: 288
Registered: ‎03-18-2002
Message 9 of 15 (341 Views)

Re: awk - single character field separator

Mark,
I understood your request. I included the clip from the gnu site to try to explain that this is a feature of GNU only, and that UNIX awk doesn't let you do it. Sorry if that wasn't clear.

-paul
Please use plain text.
Advisor
Mark Tunnell
Posts: 33
Registered: ‎01-19-2003
Message 10 of 15 (341 Views)

Re: awk - single character field separator

Thanks for all the responses. I'll go ahead and user perl for this.
Please use plain text.
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 11 of 15 (341 Views)

Re: awk - single character field separator


Here you go:


echo hello | awk '{gsub("(.)","& " );print $1;}'


The POWER of regex's!!!


live free or die
harry d brown jr
Live Free or Die
Please use plain text.
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 12 of 15 (341 Views)

Re: awk - single character field separator

or

echo hello | sed "s/\(.\)/& /g" | awk '{printf("f1(%s) f2(%s) f3(%s) f4(%s) f5(%s)\n",$1,$2,$3,$4,$5);}

f1(h) f2(e) f3(l) f4(l) f5(o)

or
echo hello | sed "s/\(.\)/& /g" | awk '{print $5,$4,$3,$2,$1;}'

o l l e h


live free or die
harry d brown jr
Live Free or Die
Please use plain text.
Advisor
Mark Tunnell
Posts: 33
Registered: ‎01-19-2003
Message 13 of 15 (341 Views)

Re: awk - single character field separator

Wow! Thanks!
Please use plain text.
Esteemed Contributor
Vibhor Kumar Agarwal
Posts: 787
Registered: ‎04-05-2005
Message 14 of 15 (341 Views)

Re: awk - single character field separator

Why to complicate
We can simply use:

echo hello | sed "s/./& /g" | awk '{print $5,$4,$3,$2,$1;}'

I know we can use any general os command in awk block, but somehow i am unable to figure out how.
Can someone help me start it out.

Thanks
Vibhor Kumar Agarwal
Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 15 of 15 (341 Views)

Re: awk - single character field separator

lt09:/home/merijn 110 > echo hello | perl -lne'$,=" ";print split//'
h e l l o
lt09:/home/merijn 111 > echo hello | perl -lne'$,=" ";print reverse split//'
o l l e h
lt09:/home/merijn 112 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation