Command-Line AWK 
Author Message
 Command-Line AWK

Hate to do this, but could anyone direct me to any good resource on using
AWK on the command line (especially)?  Found some cool stuff in the manpage
(actually GAWK), but it seems to focus more on making executable (G)AWK
files.  Don`t use it for that, at least right now.  Just started looking at
it over the weekend and already it`s great, but I want to know a bit more
about using it on the command line.

Thanks for anything.

--

Joshua Lilly
Composers Historical Society

http://www.*-*-*.com/



Sat, 10 Mar 2001 03:00:00 GMT  
 Command-Line AWK

Quote:

>Hate to do this, but could anyone direct me to any good resource on using
>AWK on the command line (especially)?  Found some cool stuff in the manpage
>(actually GAWK), but it seems to focus more on making executable (G)AWK
>files.  Don`t use it for that, at least right now.  Just started looking at
>it over the weekend and already it`s great, but I want to know a bit more
>about using it on the command line.

Just do it.  :-)

Put the stuff in single quotes is the usual way.

gawk 'BEGIN{print "start"}{print NF;print NF, $0}END{print "The End}' infile

try that on a small file and go experiment from there.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 10 Mar 2001 03:00:00 GMT  
 Command-Line AWK

Quote:

>Hate to do this, but could anyone direct me to any good resource on using
>AWK on the command line (especially)?  Found some cool stuff in the manpage
>(actually GAWK), but it seems to focus more on making executable (G)AWK
>files.  Don`t use it for that, at least right now.  Just started looking at
>it over the weekend and already it`s great, but I want to know a bit more
>about using it on the command line.

It's a little ambiguous what you mean, but there are several approaches:

o The command line may include the (short, usu. 1-liner) script, or
o the command line may call a script with the -f option.

e.g.,

gawk '/foo/ {print NR, $0}' textfile

        will print out each line containing "foo" (like grep) and will
        prepend the number of the line in the file.

gawk -f findit.awk textfile

        will do the same thing, if findit.awk contains only the line

/foo/ {print NR, $0}

AWK normally interfaces seamlessly with your command processor, allowing
pipes and redirects to work as per.  So, these should work:

gawk '/foo/ {print RN, $0}' < textfile

gawk '/foo/ {print RN, $0}' < textfile > outfile

gawk '/foo/ {print RN, $0}' < textfile | more

cat textfile | gawk '/foo/ {print RN, $0}' > outfile

Cet etera.

The awk manual page, available on a number of sites, and the awk faq, ditto,
will help a lot.  Books?  A, W and K's _The_AWK_Programming_Language_ is the
standard, and Dougherty's "sed and awk" (O'Reilly & Associates) is
excellent, too.

You say you don't do this (yet), but:

The command line interface may be improved by making the script
"executable", which is approached in different ways, depending on your
environment.

Under UN*X, you make the first line of the script:

#! /usr/bin/gawk -f

        where you supply the FULL PATHNAME to your interpreter.  This may
not run under all UN*X shells, and I haven't time now to look up the
details.  "It's in the literature."  Should be in either book listed above,
and perhaps in the awk man page.  In any event, if findit.awk is on your
path and contains the first line above, just doing

findit.awk textfile

will perform as before.  Simplify things by changing the filename to just
findit.  It still works.  You _will_ have to change the file permissions to
make it executable.

Under MSDOS, you can write a front-end batch file to invoke it; there may be
a simpler way.  If you use 4dos, 4NT or 4OS/2, you can define an "executable
extension" of, say, "awk".  Now you set .awk=gawk -f (assuming gawk.exe is
in your path, or you can put the full pathname in the set statement) and
makesure findit.awk is also on your path or in the current directory, and

findit textfile

will be parsed, findit.com, findit.exe and findit.bat will not be found, so
the command interpreter will search for findit.??? where ??? will be any
defined excutable extensions, which will include awk.  It finds findit.awk,
and actually generates the call

gawk -f findit.awk textfile

and things proceed as before.

There, I've answered some questions.  Maybe I even answered yours.

                                G'luck,
                                    d
--
                 "The weirdos will save the world in the end."
                                -- Dr. Richard Berthold



Thu, 15 Mar 2001 03:00:00 GMT  
 Command-Line AWK

% Under UN*X, you make the first line of the script:
%
% #! /usr/bin/gawk -f
%
%       where you supply the FULL PATHNAME to your interpreter.  This may
% not run under all UN*X shells, and I haven't time now to look up the
% details.  "It's in the literature."  Should be in either book listed above,

On the Unix systems I've used, this is handled by the kernel, not the
shell. An executable script should work regardless of what program is
invoking it. It will work from an execv() call in a C program.

% Under MSDOS, you can write a front-end batch file to invoke it; there may be
% a simpler way.  If you use 4dos, 4NT or 4OS/2, you can define an "executable
% extension" of, say, "awk".  Now you set .awk=gawk -f (assuming gawk.exe is

Under NT, this can be done using the assoc and ... some other command which
doesn't come to mind. (try assoc /? and see if it gives a clue). Anyway,
one of the two commands associates a command with a type of file, and the
other associates the type of file with a file extension. You the put the
extension into an environment variable called PATHEXT, and the system will
search for, eg, findit.awk when you type findit. The drawback of this is
that stdin and stdout redirection simply don't work when started using this
mechanism.

Under OS/2, you can create a .cmd file with a first line something like this
 extproc awk -f %*
and, provided you have a specially modified version of awk which ignores
a first line starting with extproc, it will work like an ordinary batch file.

--

Patrick TJ McPhee
East York  Canada



Sat, 17 Mar 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Passing command line variables to AWK in shell script

2. Awk Command-line Variables - Thanks!

3. Awk Command-Line Variables

4. Command line length - awk on a PC

5. Command line variable assignment in awk

6. pass regexp to awk on command line

7. Command line options in awk scripts?

8. Help passing command line parameters to awk scripts

9. THANKS: Help passing command line parameters to awk script

10. awk -- pattern match a line and the line that follows

11. reading a line and executing a command on that line

12. Win2000 Gawk Renaming a file with system() command, command line to long

 

 
Powered by phpBB® Forum Software