Operating System - HP-UX
1753420 Members
5001 Online
108793 Solutions
New Discussion юеВ

awk - single character field separator

 
SOLVED
Go to solution
Mark Tunnell
Advisor

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}
14 REPLIES 14
Paul Cross_1
Respected Contributor

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
Mark Tunnell
Advisor

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?
Robert-Jan Goossens
Honored Contributor

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
Stephen Keane
Honored Contributor

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}'

S.Rider
Regular Advisor

Re: awk - single character field separator

awk -F":"
Ride Boldly Ride, but watch out for El Dorado's
Stephen Keane
Honored Contributor

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'.
Rick Garland
Honored Contributor

Re: awk - single character field separator

Are other options available?

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

What about sed?
Paul Cross_1
Respected Contributor

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
Mark Tunnell
Advisor

Re: awk - single character field separator

Thanks for all the responses. I'll go ahead and user perl for this.