- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- awk vs. perl ... print $1?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2003 02:36 PM
тАО06-23-2003 02:36 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2003 03:07 PM
тАО06-23-2003 03:07 PM
Re: awk vs. perl ... print $1?
# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2003 03:49 PM
тАО06-23-2003 03:49 PM
Re: awk vs. perl ... print $1?
do a find for these scripts.
live free or die
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2003 03:52 PM
тАО06-23-2003 03:52 PM
Re: awk vs. perl ... print $1?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2003 03:54 PM
тАО06-23-2003 03:54 PM
Solutionis 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2003 03:56 PM
тАО06-23-2003 03:56 PM
Re: awk vs. perl ... print $1?
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.
- Tags:
- a2p
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2003 04:00 PM
тАО06-23-2003 04:00 PM
Re: awk vs. perl ... print $1?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2003 11:55 PM
тАО06-23-2003 11:55 PM
Re: awk vs. perl ... print $1?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-24-2003 06:03 AM
тАО06-24-2003 06:03 AM
Re: awk vs. perl ... print $1?
Once again, Procura, you have come through.
I'm not certain I would have caught the obfuscation with @F[0]!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-25-2003 04:09 AM
тАО06-25-2003 04:09 AM
Re: awk vs. perl ... print $1?
you forgot the //.
awk -F: '// {print $1 }' /etc/passwd
and what about cut?
cat /etc/passwd | cut -d: -f1
steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-14-2003 08:54 AM
тАО07-14-2003 08:54 AM
Re: awk vs. perl ... print $1?
Could you clarify the meaning of the //?
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-14-2003 12:09 PM
тАО07-14-2003 12:09 PM
Re: awk vs. perl ... print $1?
perl -aF: -nel 'print $F[0]'/etc/passwd
Should work.
Caesar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-17-2003 06:25 AM
тАО07-17-2003 06:25 AM
Re: awk vs. perl ... print $1?
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 '
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-17-2003 06:34 AM
тАО07-17-2003 06:34 AM
Re: awk vs. perl ... print $1?
// is implied so...
awk '{print $1}' file
is the same as
awk '//{print $1}'
I never use // & my awk works fine!!!!
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-17-2003 06:34 AM
тАО07-17-2003 06:34 AM
Re: awk vs. perl ... print $1?
// is implied so...
awk '{print $1}' file
is the same as
awk '//{print $1}' file
I never use // & my awk works fine!!!!
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-17-2003 06:40 AM
тАО07-17-2003 06:40 AM
Re: awk vs. perl ... print $1?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-28-2006 11:33 PM
тАО03-28-2006 11:33 PM
Re: awk vs. perl ... print $1?
Try this! Much more readable.
cat /etc/passwd | perl -anF: -e 'print $F[0],"\n"' -
Substitute the 0 for whichever column you want.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-01-2006 09:33 AM
тАО04-01-2006 09:33 AM
Re: awk vs. perl ... print $1?
During one of the forum upgrades however, all ['s got translated into their html [ values, and ]'s into ].
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-02-2006 06:25 PM
тАО04-02-2006 06:25 PM
Re: awk vs. perl ... print $1?
Here's me thinking I was being clever.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-22-2006 06:09 AM
тАО05-22-2006 06:09 AM