Passing parameters through pure awk and awk scripts

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.