From: Janis on
On 9 Feb., 14:23, Ed Morton <mortons...(a)gmail.com> wrote:
> On 2/8/2010 10:09 PM, Phred Phungus wrote:
>
>
>
>
>
> > Ed Morton wrote:
> >> On Feb 8, 7:23 pm, romy <rav...(a)gmail.com> wrote:
> >>> this one is pretty easy for most of you guys.
>
> >>> Format the date present in a csv file to date format MM/DD/YYYY
>
> >>> i/p : 1/1/1900
> >>> o/p : 01/01/1900 or 01-01-1900
>
> >>> i/p : 10/2/2009
> >>> o/p : 10/02/2009 or 10-02-2009
>
> >>> Thanks !
>
> >> I have a feeling this will turn into a "thanks but my input REALLY
> >> looks like this..." thread, but maybe this will be enough to get
> >> started:
>
> >> $ echo "1/1/1900" | awk -F'/' '{printf "%02d-%02d-%4d\n",$1,$2,$3}'
> >> 01-01-1900
>
> > I'm glad to see this post as I was reading up on perl, and awk is
> > mentioned more than once. I was nonplussed when I looked in the
> > reference for functions not to find it. I gathered that its
> > functionality had been incorporated into perl in a very central way.
>
> awk and perl are 2 totally different tools. I'm no perl expert but AFAIK:
>
> awk is for text processing while perl is for text processing plus additional
> things that you'd typically have do in shell (or some other tool) if you're
> using awk for the text processing part.
>
> The awk syntax for text processing is clear and simple to anyone with any
> procedural programming experience while the perl syntax is challenging.
>
> awk is available on every UNIX machine, perl isn't but can be installed.
>
> >http://www.opengroup.org/onlinepubs/009695399/utilities/awk.html
>
> > Apparently the -F does this:
>
> > Define the input field separator to be the extended regular expression ERE
>
> > So the -F'/' says split on the slash. I recognize the formats,
> > conversions and variables in the printf, but what's person to think of
> > the whole '{ ... }' syntax?
>
> awk is designed for text processing. What's the 1 thing you always have to do
> when processing text? Loop over the text. So, rather than everyone having to
> write the loop, the awk tool itself provides it so you just have to write what
> you want to do for every record (line by default) of input.

In perl you can specify an option -n to behave in a similar way.

Janis

> [...]

From: Maxwell Lol on
Ed Morton <mortonspam(a)gmail.com> writes:

> awk and perl are 2 totally different tools. I'm no perl expert but AFAIK:
>

I like to think of perl as a superset of sed and awk.

there is a a2p script that comes with perl that translates awk into perl.
(there's also a s2p (sed-to-perl) script.


From: Ed Morton on
On 2/12/2010 6:05 AM, Maxwell Lol wrote:
> Ed Morton<mortonspam(a)gmail.com> writes:
>
>> awk and perl are 2 totally different tools. I'm no perl expert but AFAIK:
>>
>
> I like to think of perl as a superset of sed and awk.

....plus other things typically handled by the shell around the sed/awk scripts.
In that vein, though, you could say that awk is a superset of sed + other things
so saying perl is a superset of sed and awk has some redundancy.

>
> there is a a2p script that comes with perl that translates awk into perl.
> (there's also a s2p (sed-to-perl) script.

Do those tools convert the sed/awk to idiomatic perl or just to some pile of
perl that will produce the desired output?

Here's why I ask: there's a tool called "awkcc" that converts awk to C but the
resultant C is typically very different from the C that you'd write by hand in
terms of modularity, readability, etc. so you would never take that and build on
it in future and after compilation it doesn't run noticably faster than the
original awk anyway so I've never been entirely sure what the point of it is.

I suspect the same would be true converting any but the simplest awk scripts
from awk to perl using the tool you mentioned, but if the script was simple you
wouldn't need a tool to convert it so again - what's the point?

Ed.
From: pk on
Ed Morton wrote:

>> there is a a2p script that comes with perl that translates awk into perl.
>> (there's also a s2p (sed-to-perl) script.
>
> Do those tools convert the sed/awk to idiomatic perl or just to some pile
> of perl that will produce the desired output?

It seems it's not too bad, though it's certainly not perfect. Two examples
follow:

$ echo 'ORS=NR%7?RS:FS' | a2p
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches

$FS = ' '; # set field separator
$\ = "\n"; # set output record separator

while (<>) {
chomp; # strip record separator
print $_ if $\ = $. % 7 ? $/ : $FS;
}
----------------------------------
$ echo 'BEGIN{FS="a|b|c"}NR==FNR{a[$1];next} $0 in a' | a2p
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches

$[ = 1; # set array base to 1
$FS = ' '; # set field separator

$FS = 'a|b|c';

line: while (<>) {
chomp; # strip record separator
@Fld = split($FS, $_, -1);
if ($. == ($.-$FNRbase)) {
$a{$Fld[1]};
next line;
}
print $_ if defined $a{$_};
}
continue {
$FNRbase = $. if eof;
}
----------------------------------

Unfortunately, it seems it has some bugs:

$ echo '{x=X+5;print x}' | a2p
....
while (<>) {
$X = $X + 5;
print $X;
}

(note the two variables x and X are translated both into $X). Oddly, using
another letter different from x is translated correctly.

The man page for a2p does a good job of explaining what a2p can and cannot
do when translating from awk.
From: Maxwell Lol on
pk <pk(a)pk.invalid> writes:

> Ed Morton wrote:
>
>>> there is a a2p script that comes with perl that translates awk into perl.
>>> (there's also a s2p (sed-to-perl) script.
>>
>> Do those tools convert the sed/awk to idiomatic perl or just to some pile
>> of perl that will produce the desired output?
>
> It seems it's not too bad, though it's certainly not perfect. Two examples
> follow:

snip.

When I first started learning perl, converting a multi-page awk script
into perl was a big help. In those days, awk had some hard limits,
like the number of fields. a2p was a quick fix to a problem where
previously I had to change the source code of AWK (before Gawk) to fix.