Invoking awk in a shell script is very natural and simple, and if you need to invoke the shell script/command, you need to use the system () function, which is written as System ("sh my.sh" $var) if you need to pass the variable to the called Shell. Note that there is a space before the second quotation mark.Awk calls the shell and passes the variable to the shell, and looking at the demo below, it's clear:
The code is as follows
Copy
Using the shell to combine awk is a good way to tell if a process is running or if a program file is being executed, as an example of PHP.
Define a crontab, execute every minute, the time interval is one minute, possibly next time executes, the last time did not finish, therefore we must make the judgment.
1,test.php in the case of running, view process
# Ps-ef |grep test.phpRoot 29884 29881 0 10:48? 00:00:00 a
/bash Action{print $7} is specified here.awk built-in variablesAwk has many built-in variables for setting up environment information, which can be changed, and some of the most commonly used variables are given below.ARGC command-line arguments argv command-line parameter arrangement environ support the use of system environment variables in queues filename awk browses the file name Fnr the
execution process of awk is also. Input text, match pattern, execute action, then match pattern, and then execute action. Note: awk has no mode space.
Its actions are also different from those of sed.
2: Pattern
In awk's eyes, a text is seen as a table.
In details, awk uses the RS string to cut the data into one row regardless of the text, and then uses FS to cu
Run the operating system command and return it to the awk function. The system function executes the command specified by string and returns the status returned by the command as its value.
For example
CodeSegment
End {
System ("date | mail-s 'awkrundone 'root ")
}
The system administrator
ProgramAfter the input is processed, an email is sent.
Awk programs are widely used to process log files with
'/inet addr/{print $2}' | awk-F: '{print $2}' $ ifconfig | awk '/inet addr/{print $2}' | awk-F: 'In in {print "BEGIN .. "} {print $2} 'end {print" END... "} 6 Pattern matching 1) use regular expression 2) use a Boolean (comparison) expression. when the expression value is true, perform the corresponding operation (actions) variables (such as field variables $1 a
can use positional variables to specify the fragment to be output. Usage: awk [Options] ' {print '} ' file ... #支持位置参数 Example Output First Fragment awk ' {print '} ' test.txt Output print for awk 1. When you want to output multiple variables, the Print command uses {default, "comma"} to split, and when output, the default space is split 2. The output item can
Getline of awkTo learn more about getline, you must first understand the concepts of $0, NR, and FNR.The so-called NR can be interpreted as Number of Record, or a row Number, while FNR can be interpreted as Number of Record in File or a row Number in the File. It sounds no different. In fact, if you only have one file for awk processing,
contents of column 4th of line 3rd
SSH 192.168.1.101 ' free-g ' | awk ' Nr==3{print $} '
Timed End Process (This example ends in 5 seconds)
{sleep 5; eval ' kill-9 $! ';}
Query remote server memory, print out the contents of line 3rd, column 4th, no matter whether the connection is successful or not, it will end in 5 seconds.
SSH 192.168.1.101 ' free-g ' | awk
#AWK命令基础显示
Print the second area of the Install.log file that contains the data field row
awk ‘/data/ {print $2}’ install.log
View the first line of Num10.txt
head -n 1 num10.txt结果:10cat !$ #cat上次打开的文件,cat num10.txt
AWK displays only the first line:
NR number of rows in row
lines in a file.Gawk ' BEGIN {for (i = 1; I print INT (101 * rand ())} 'This program shows a range of 7 random numbers from 0 to 100.Ls-l Files | Gawk ' {x + = $4}; END {print "Total bytes:" x} 'This program displays the total number of bytes for all specified files.Expand File | Gawk ' {if (x END {print "Maximum line length is" x} 'This program displays the length of the longest line in the specified file. Expand will change the tab to space, so the actual right boundary is used to make the le
the entire record. $ $ represents the first field of the current row, which is the second field of the current row,...... And so on1. Statistics/etc/passwd: File name, line number per line, number of columns per row, corresponding full line contents:#awk-F ': ' {print ' filename: ' filename ', linenumber: ' NR ', columns: ' NF ', linecontent: ' $ '/etc/passwd2. Use printf instead of print to make your code
the number of domains in the command line-F option NF browsing record Nr number of records read OFS output domain separator ors output record separator Rs control record Separator
Instance
[[emailprotected]~]$awk -F ‘:‘ ‘{print "filename:" FILENAME ",linenumber:" NR ",columns:" NF}‘ /etc/passwdfilename:/etc/passwd,linenumber:1,columns:7filename:/etc/passwd,lin
end of the line: $ awk '$2 =75 {print $0;} 'testfileproducta 30 REORDERProductB 76 ProductC 55 REORDER it can be seen that awk has a very similar printf function with C language. The condition part of the awk command can also be two special condition-BEGIN and END. for each file to be processed, actions after BEGIN will be executed once before processing the ent
Application of awk built-in Variables
Now let's look at some examples. They start with the variable NR. Modify the print statement in the script for calculating the average score:$ Cat grades
John 85 92 78 94 88
Andrea 89 90 75 90 86
Jasper 84 88 80 92 84
$ Cat grades. awk
# Calculate the average value of five scores
{Total = $2 + $3 + $4 + $5 + $6
Avg = total/5
Format: awk '{Pattern + action}' {filenames}
Common parameters:
-F specifies that the Delimiter is a space by default.
Start with begin printing
End print end
Built-in Variables
Number of argc command line parameters
Argv command line parameter arrangement
Environ supports the use of system environment variables in the queue
Filename awk browsed file name
Number of FNR browsing file records
FS sets the inpu
Introduction to linux awk built-in variables awk is an excellent text processing tool and can be said to be a programming language. The following are the built-in variables of awk. 1. built-in variable table attribute description $0 current record (as a single variable) $1 ~ $ N indicates the nth field of the current record. The number of fields in the current re
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.