Operating System - Linux
1822229 Members
3772 Online
109642 Solutions
New Discussion юеВ

Re: awk vs. perl ... print $1?

 
SOLVED
Go to solution
A. Daniel King_1
Super Advisor

awk vs. perl ... print $1?

Hi, perl gurus.

If someone could point me to the best/shortest way to convert a simple awk script to perl, I will relegate awk to the mental dustbin of seldom-used commands.

$awk -F: '{ print $1 }' /etc/passwd

a2p returns something absolutely ghastly for this one-liner. Is there a way, oh perl gurus, to repeat the elegance of the above awk solution in perl?

Thanks.
Command-Line Junkie
19 REPLIES 19
Geoff Wild
Honored Contributor

Re: awk vs. perl ... print $1?

Hmmnnn...this may help a bit:

# Sort /etc/passwd by user name.

$sort = Sort::Records->
new([width => 10,
split => [':', 0]]);
@pw = $sort->sort(`cat /etc/passwd`);

# Sort /etc/passwd by user ID.

$sort = Sort::Records->
new([type => 'int',
split => [':', 2]]);
@pw = $sort->sort(`cat /etc/passwd`);



Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Donny Jekels
Respected Contributor

Re: awk vs. perl ... print $1?

well belive it or not your perl distribution should have come with awk2perl, and a sed2perl convertor.

do a find for these scripts.


live free or die
"Vision, is the art of seeing the invisible"
Mike Stroyan
Honored Contributor

Re: awk vs. perl ... print $1?

I'm not a perl guru, but ...
I want to play too! ;-)

perl -anF: -e 'print @F[0],"\n"' /etc/passwd

Of course, I would probably really use

cut -d: -f1 /etc/passwd
H.Merijn Brand (procura
Honored Contributor
Solution

Re: awk vs. perl ... print $1?

# perl -aF: -nle'print $F[0]' /etc/passwd

is exactly the same as one-liner

-a autosplit to @F
-F: split on : instead of whitespace
-n don't print for every line
-l print newlines after every print (not for printf's)
-e use perl expression

$F[0] is the first field, perls lists/arrays are indexed starting from 0

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Donny Jekels
Respected Contributor

Re: awk vs. perl ... print $1?

here it is.

http://www.perl.com/doc/manual/html/x2p/a2p.html

I used it to convert over 100 awk scripts and ran into only but a handfull errors. which needed only a little editing.
"Vision, is the art of seeing the invisible"
Donny Jekels
Respected Contributor

Re: awk vs. perl ... print $1?

and here is the link to sed to perl.


http://www.ictp.trieste.it/texi/perl/perl_72.html


hey sorry for the earlier post. about awk2perl and sed2perl.

its a2p and s2p, which are free translators and free to download.

good luck with your translation expidition.

live free or die
Donny
"Vision, is the art of seeing the invisible"
H.Merijn Brand (procura
Honored Contributor

Re: awk vs. perl ... print $1?

Donny, why do you litter this thread with exactly what he does *NOT* want. Read the original question again please.

Mike, are you using perl6? Though @F[0] is valid use in perl5, it is an array slice of only one element, it's use in your context is highly obfuscating.

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
A. Daniel King_1
Super Advisor

Re: awk vs. perl ... print $1?

Thanks, everyone for your posts.

Once again, Procura, you have come through.

I'm not certain I would have caught the obfuscation with @F[0]!
Command-Line Junkie
Steve Post
Trusted Contributor

Re: awk vs. perl ... print $1?

an AWK fyi here.
you forgot the //.
awk -F: '// {print $1 }' /etc/passwd

and what about cut?
cat /etc/passwd | cut -d: -f1

steve
A. Daniel King_1
Super Advisor

Re: awk vs. perl ... print $1?

Steve,

Could you clarify the meaning of the //?

Thanks.
Command-Line Junkie
Caesar_3
Esteemed Contributor

Re: awk vs. perl ... print $1?

Hello!

perl -aF: -nel 'print $F[0]'/etc/passwd
Should work.

Caesar
Steve Post
Trusted Contributor

Re: awk vs. perl ... print $1?

I thought this message had already run its course. Sorry. About that.

On the "//" in the Awk command.
The // means "for every line."

You didn't have // in your awk script. I don't know what it was doing.

Normally I use awk this way.

cat file | awk ' { action to do}'

You gave awk an action to do, but did not select any lines?

other selections.
if field 2 is not billy, print the line.
awk '$2 !~ /billy/ { print $0 }'

if the line begins with hoohay, print field 3.
awk '/^hoohay/ { print $3 }'

print something at the start and end.
awk 'BEGIN { print "this is the start.\n" }
// { printf("here is a line %s\n",$0) }
END { print "this is the end." }'

steve.
Tim D Fulford
Honored Contributor

Re: awk vs. perl ... print $1?

Hi

// is implied so...
awk '{print $1}' file
is the same as
awk '//{print $1}'

I never use // & my awk works fine!!!!

Tim
-
Tim D Fulford
Honored Contributor

Re: awk vs. perl ... print $1?

Hi

// is implied so...
awk '{print $1}' file
is the same as
awk '//{print $1}' file

I never use // & my awk works fine!!!!

Tim
-
Steve Post
Trusted Contributor

Re: awk vs. perl ... print $1?

ok Tim. I bet you're right. But I'm not the author. I can't see what version of awk he's using.
Maybe // does not have to be there.
I just put it in out of habit. In any case, you see it means "grab all lines."
steve
Brian Burroughs
New Member

Re: awk vs. perl ... print $1?

Old postings I know but...
Try this! Much more readable.

cat /etc/passwd | perl -anF: -e 'print $F[0],"\n"' -

Substitute the 0 for whichever column you want.
Stuart Browne
Honored Contributor

Re: awk vs. perl ... print $1?

Ugh. Actually, that is basically what procura and Mike's posts said.

During one of the forum upgrades however, all ['s got translated into their html [ values, and ]'s into ].
One long-haired git at your service...
Brian Burroughs
New Member

Re: awk vs. perl ... print $1?

LOL

Here's me thinking I was being clever.
A. Daniel King_1
Super Advisor

Re: awk vs. perl ... print $1?

Not quite as elegant, but probably more extensible. Thanks, all.
Command-Line Junkie