awk study(2)

来源:互联网 发布:spss软件怎么安装 编辑:程序博客网 时间:2024/05/16 13:00
1.1.2 Running awk without input file
       You can also run awk without any input files, if you type the follow command line
        awk 'program'
        awk appies program to the standard input, which usually means whatever you type on the terminal, this continues until you indicate end-of-file by typing Ctrl+d(on the other operating systems, end-of-file charaters may be different).
      As an example, the following program prints a friendly piece advice,to keep you from worring about the complexities of computer programming.
      $awk "BEGIN {print /"Don't Panic/"}"
      |  Don't Panic
      This program doesn't read any input file. The character '/'before each of the inner double quote is necessary because of the shell's quoting rules---in particular it mixes both single quotes and double quotes.
    The next simple awk program emulates the cat utility,it copies whatever you type on the keyboard to its standard output.
     $ awk '{ print }'
Now is the time for all good men
-| Now is the time for all good men
to come to the aid of their country.
-| to come to the aid of their country.
Four score and seven years ago, ...
-| Four score and seven years ago, ...
What, me worry?
-| What, me worry?
Ctrl-d

1.1.3 Running long programs

Sometimes your awk programs can be very long,it is more convenient  to put  the programs  into  a  separate  file, in order to tell awk to use that file  for its program, you type :

        $awk  -f   source-file  input-file1  input-file2

The -f instruct the awk utility to get the program from the file source-file, any file name can be used for the source-file.For example, you could put the program :

        BEGIN {print "Don't Panic !"}

into a file advice , Then this command :

       $awk -f advice 

does the same thing as this one:

     $awk "BEGIN {print /"Don't Panic/"}"

This was explained earlier, note that you don't usually need sigle quotes around the file name that you specify with -f, because most file names don't contain any of the shell's special charaters.Notice that in advice, the awk program don't have single quotes around it, the quotes are only need for programs that are provied on the awk command line.

      If you want to identify your awk program files  clearly as such, you can add the extension .awk to the file name, this doesn't affect the execution of awk program but it does make  housekeeping easier.