in reply to Convert from awk to perl

I have a shell script with that uses the following awk section that I would like to put into perl.

I'm going to run counter to what others here have said by telling you that 'a2p' will not do that. It may well produce some Perl code that will imitate what you were doing with 'awk' - but you'll gain nothing in terms of knowledge, speed, maintainability, readability, or anything else I can think of. (Do feel free to skip the rest of this rant "Meditation in the Wild" if you were just doing this for some sort of Perl usage coolness points or on a bet or something like that.)

If, on the other hand, you actually wanted to learn some Perl by rewriting this - that's something that I'd see as a valuable goal. Perl has a much more comprehensive toolbox than 'awk' does, so if you had been trying to chop down a tree with an 'awk' jacknife, you could use a Perl jacknife, too... but you'd be much, much better off with the Perl chainsaw. Or Perl dynamite. Or a Perl Caterpillar D9.

As an example, the validation routine that you have at the top of your script would benefit from rethinking (as well as rewriting in Perl) quite well.

# Original 'awk' code { process=0 if ( substr($3,1,3) == "eth" ) process=1 if ( substr($3,1,3) == "bon" ) process=1 if ( $3 == "eth0" ) process=1 if ( ! ( $2 == "AM" || $2 == "PM" ) ) process=0 if ( $3 == "IFACE" ) process=0 if ( process == 0 ) next [...]

Since Perl does not automatically loop over the input file (but do see 'perldoc perlrun' for the '-n' switch) or split the input into fields (but see 'perldoc perlrun' for the '-a' switch), we'll need to do those explicitly. After that, though, things get to be fun. :)

# Perl version while (<>){ # Loop over the input file/stream my @line = split; # Split each line into "fields" next unless $line[1] =~ /^[AP]M$/ && $line[2] =~ /^(eth|bon)/; # Note: the 'eth0' and the 'IFACE' line aren't in here because the +y don't # contribute anything to the validation routine as originally writ +ten. [ ... ]

That was it; essentially, that whole routine became just one line. The rest of the script can be rewritten in much the same way, with as much or more benefit in clarity, readability, etc.

My point, overall, is that some scripts - particularly simple ones like this, and especially if you've already found the motivation to do so - should be rewritten every once so often, whether in other languages or in the original one. At the very least, this will show you if you've learned anything about those languages in the meantime - or let you learn something new by doing a task you already understand in a different way.

If you decide to do that, feel free to come back here and ask for help. This is, in my experience, one of the friendliest forums out there when it comes to helping a struggling Perl neophyte - at least if you can show what you've coded so far. Give it a shot!


-- 
Human history becomes more and more a race between education and catastrophe. -- HG Wells