Simpler Navigation coming for Servers and Operating Systems
Coming soon: a much simpler Servers and Operating Systems section of the Community. We will combine many of the older boards, and you won't have to click through so many levels to get at the information you need. If you are looking for an older board and do not find it, check the consolidated boards, as the posts are still there.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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?
Highlighted
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.
harry d brown jr
Honored Contributor
Solution

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
harry d brown jr
Honored Contributor

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

Re: awk - single character field separator

Wow! Thanks!
Vibhor Kumar Agarwal
Esteemed Contributor

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
H.Merijn Brand (procura
Honored Contributor

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