BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

awk - single character field separator

SOLVED
Go to Solution
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
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
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?
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
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}'

Regular Advisor

Re: awk - single character field separator

awk -F":"
Ride Boldly Ride, but watch out for El Dorado's
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'.
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?
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
Advisor

Re: awk - single character field separator

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

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

Re: awk - single character field separator

Wow! Thanks!
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
Highlighted
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