The problem of passing parameters in awk has always been a headache for many people. The following is a summary:
I. Direct call of awk scripts .
For example, the following script requires the calculation of the total number of logs on the 15th to 30th. $3 indicates the domain that distinguishes the encoding of different pages. The script is as follows:
#/Bin/sh;
Page = $1;
I = 15;
While test $ I-le 30
Do
# Awk '{M =' "$ J" '; print $ m }'
Res = $ (awk-F "\ t" '{if ($3 = P) {print $0} 'P = $ page shop_page.log.2009-07-$ I-* | WC-L );
Echo $ I "" $ res;
(I = $ I + 1 ))
Done
Take out the awk script,Awk-F "\ t" '{if ($3 = P) {print $0}' p = $ page shop_page.log.2009-07-$ I -*
Where p acts as the lead of the needle, that is, the parameter is passed in, and its formIsAwk-f '{script body}' parameter = value log name
There is also a numeric type, there is no need for such parameter values to be placed behind
For exampleCurrentserver =$ (echo $ server | awk '{M =' "$ J" '; print $ m }');
$ J is the value in the shell script, which directly prints the column fields equal to J. Parameter passing is not required
Ii. Call awk Script Type
Sometimes the awk script needs to be called directly in the shell script. Parameters can be passed as follows:
Awk-F awk script. awk statdate = "$ statdate" stathour = "$ stathour" log name. Log
Statdate and stathour are input parameters, where awk can be directly used
Date = statdate;
Hour = stathour;