Do you ever use awk?

K

kj

Do people proficient in Perl have any use for awk/gawk anymore?
Are there things that you still prefer to do using awk/gawk over
perl?

Thank you for your opinions!

kj
 
J

Jeff Schwab

kj said:
Do people proficient in Perl have any use for awk/gawk anymore?
Are there things that you still prefer to do using awk/gawk over
perl?

I use awk occasionally for pulling particular columns out of a sequence
of records, e.g. awk '{ print $3 }' to get the third entry on each line
of a file.
 
R

Richard Gration

Do people proficient in Perl have any use for awk/gawk anymore? Are
there things that you still prefer to do using awk/gawk over perl?
Thank you for your opinions!
kj
[/QUOTE]

I only started using unix (Linux) about 5 or 6 years ago so the main
thing I get out of using awk is a sense of history ;-)

Seriously, I tend to use awk when I'm doing lots of things through a bunch
of shell pipes. eg

ls *.txt | awk 'fname = $0; gsub(/_old/,"_new"); print fname " " $0;' |
xargs -n2 mv

I s'pose it could be done with perl too <shrug>.

Rich
 
B

Ben Morrow

Quoth kj said:
Do people proficient in Perl have any use for awk/gawk anymore?
Are there things that you still prefer to do using awk/gawk over
perl?

My system (gentoo linux) has awk in /bin and perl in /usr/bin, so it
uses awk for a lot of stuff in the system startup scripts that I'd
probably have used Perl for if possible (and Gentoo'd probably've used
Python, but that's their loss... :).

Ben
 
C

ctcgag

kj said:
Do people proficient in Perl have any use for awk/gawk anymore?
Are there things that you still prefer to do using awk/gawk over
perl?

If anything, I now use awk even more, and more proficiently. (Some places
the Perl docs will compare some feature to awk, things that I never knew
awk could do in the first place. But now that I know, I use it.).

But mostly I still use it for getting a few columns. I find that with
Perl I often forget either the 'l' or the 'n' in -lnae, or I forget that
'a' splits into @F, not @_ or @a, or forget that the 'e' has to be last
switch in the switch list. It is easier to get awk right on the first try,
for me.

Xho
 
S

Scott Stearns

You might end up working at a place where people don't really use Perl and a
lot of the stuff is written with shell scripting. In that case, you'd see a
lot of awk. It is neat to work with those Unix tools...it's rewarding when a
script that looks like Greek actually does what you wanted it to. :)

-Scott
 
U

Uri Guttman

SS> You might end up working at a place where people don't really use
SS> Perl and a lot of the stuff is written with shell scripting. In
SS> that case, you'd see a lot of awk. It is neat to work with those
SS> Unix tools...it's rewarding when a script that looks like Greek
SS> actually does what you wanted it to. :)

in my pre-perl days, i did tons of shell/awk stuff. i am so glad i don't
have to go back to awk. :)

uri
 
M

Michal Jaegermann

Jeff Schwab said:
I use awk occasionally for pulling particular columns out of a sequence
of records, e.g. awk '{ print $3 }' to get the third entry on each line
of a file.

For that "complicated" usage you do not need awk at all (or perl for
that matter)

while read a b c d ; do echo $c ; done < my_file

A "fancier" /bin/sh solution employs a function which does 'echo $3' or
'shift $1 ; echo $1' if a column you want has a number higher than 9 and
you are passing a desired column number as the first argument.

Michal
 
B

Ben Morrow

Quoth Michal Jaegermann said:
For that "complicated" usage you do not need awk at all (or perl for
that matter)

while read a b c d ; do echo $c ; done < my_file

That will obey shell quoting rules when splitting, though, which may well not
be desirable.

Ben
 
L

Lukas Mai

(e-mail address removed) schrob:
[...]
If anything, I now use awk even more, and more proficiently. (Some places
the Perl docs will compare some feature to awk, things that I never knew
awk could do in the first place. But now that I know, I use it.).
But mostly I still use it for getting a few columns. I find that with
Perl I often forget either the 'l' or the 'n' in -lnae, or I forget that
'a' splits into @F, not @_ or @a, or forget that the 'e' has to be last
switch in the switch list. It is easier to get awk right on the first try,
for me.

Minor nit: '-e' doesn't have to be the last switch.

perl -e 'your program here' -lna
works just fine.

HTH, Lukas
 
B

Ben Morrow

Quoth Lukas Mai said:
(e-mail address removed) schrob:
[...]
If anything, I now use awk even more, and more proficiently. (Some places
the Perl docs will compare some feature to awk, things that I never knew
awk could do in the first place. But now that I know, I use it.).
But mostly I still use it for getting a few columns. I find that with
Perl I often forget either the 'l' or the 'n' in -lnae, or I forget that
'a' splits into @F, not @_ or @a, or forget that the 'e' has to be last
switch in the switch list. It is easier to get awk right on the first try,
for me.

Minor nit: '-e' doesn't have to be the last switch.

It has to be the last in any given group, which is Xho's problem.

perl -lena'program'

doesn't work at all.

Ben
 
J

Jeff Schwab

Michal said:
For that "complicated" usage you do not need awk at all (or perl for
that matter)

while read a b c d ; do echo $c ; done < my_file

The awk approach isn't "complicated," but what you wrote is getting there.
A "fancier" /bin/sh solution employs a function which does 'echo $3' or
'shift $1 ; echo $1' if a column you want has a number higher than 9 and
you are passing a desired column number as the first argument.

Thanks, but I try to avoid "fancier" solutions. :)
 
J

Juha Laiho

kj said:
Do people proficient in Perl have any use for awk/gawk anymore?
Are there things that you still prefer to do using awk/gawk over
perl?

I recently thought about this, and ended up having the tools set
up in three categories. The first category containing the traditional
shell tools (cat, sed, grep, sort, ...). The second category was
"what do I use when I'm doing something where this combination is
no longer convenient to solve the problem at hand"; that category
was just "awk". Then, a third category was "what to use when awk
is not enough", and this was "perl" (including needed modules on
a case-by-case basis).

This third category includes things like cases where networking or
database connectivity is needed, and also things where I do something
requiring data structures more complex than provided by the awk hashes
(mostly hierarchical data, so hashes of hashes and so).

Depending on the host OS, I've lately begun skipping the awk stage
and just doing with perl whatever doesn't feel convenient to solve
with traditional tools. This mostly is when I'm working on a Linux
system, where I can assume existence of perl.

On the other hand, on systems running proprietary Unixes (Solaris,
HP-UX), I don't take existence of perl for granted (even though
perl might be installed on that single machine where I'm writing
the new tool), so in these environments I tend to avoid using perl
much longer. But I guess using awk to write output to be executed
in dtksh eval statement (to get dynamic fill-in forms) was overdoing
it... :)
 
M

Michal Jaegermann

Jeff Schwab said:
The awk approach isn't "complicated,"

Indeed. :) Awk can do also various other things. The point
was that awk is a heavy artillery for that minor task if you are
not using it for anything else.
but what you wrote is getting there.

Ahem! If you think so. But you may use perl equally well. See
'perl -ane ...'. There is also "read -r" if you are afraid that
something may get wrongly interpreted.

Michal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top