Linux Shell Advanced Programming Tips 1---in-depth discussion (awk, <<)

Source: Internet
Author: User
Tags logical operators

1. In-depth discussion (awk, <<)
1.1. In-depth discussion of awk
Records and fields, patterns and actions, regular expressions and meta-characters
The basic tutorial has introduced
Conditional and logical operators
< less than
>= greater than or equal to
<= less than or equal to
= = equals
! = does not equal
~ Match Regular expression
!~ does not match regular expressions

&& and
|| Or
! Not

Example (Note: Www.log is a log file of Apache, which is commonly encountered in daily life)

#!/bin/bashecho "210-219 Network segment access is: ' awk ' {if ($1~/^21[0-9]/) print $} ' Www.log | Wc-l ' echo ' non-210-219 segment access is: ' awk ' {if ($1!~/^21[0-9]/) print $} ' Www.log | Wc-l ' "echo" was visited on July 07, 2004: ' awk ' {if ($4~/^\[07\/jul\/2004/) print $} ' Www.log | Wc-l ' echo ' July 07, 2004/htm/free_call.php visits are: ' awk ' {if ($4~/^\[07\/jul\/2004/) print $} ' Www.log | awk ' {if ($7== "/htm/free_call.php") print $} ' | Wc-l ' "

awk built-in variables
Built-in variables
ARGC number of command line arguments
ARGV command line parameter arrangement, is an array, with argv[0], argv[1] ... The way
ENVIRON support for the use of system environment variables in queues
FileName awk browses the file name
FNR number of records to browse files
FS set input field delimiter, equivalent to command line parameter-F option
NF browsing record number of fields
NR number of records read
OFS output Field delimiter
ORS Output Record delimiter
RS Control record delimiter
Example:
Awk-f ' # ' {print nf,nr,$0} ' Grade.txt
Awk-f ' # ' {print nf,nr,environ[' USER '],$0} ' grade.txt
Awk-f ' # ' {print nf,nr,environ[' USER '],$0,filename} ' grade.txt
Awk-f ' # ' {print nf,nr,environ[' USER '],$0,FILENAME,ARGC} ' grade.txt
Awk-f ' # ' {print nf,nr,environ["USER"],$0,filename,argc,argv[0]} ' grade.txt
Supplemental: Grade.txt File contents
85#senior
87#junior
75#senior
79#senior
92#senior
23#junior
Built-in String functions
Built-in String functions
Gsub (R,s) replaces R with s in the entire $
Gsub (R,S,T) replaces R with S in the whole t
Index (S,T) returns the first position of a string T in S
Length (s) returns the lengths of S
Match (S,R) tests if s contains a string that matches R
Split (S,A,FS) divides s into sequence a on FS
Sprint (FMT,EXP) returns the EXP formatted by FMT
Sub (r,s) replaces s with the longest leftmost substring in
SUBSTR (S,p) returns the latter part of the string s starting from P
SUBSTR (s,p,n) returns the back part of the string s from p starting at length n
Example
Awk-f ' # ' {if (Gsub ("#", "| |")) Print $} ' Grade.txt
Awk-f ' # ' {if (Gsub ("s", "s", $)) print $} ' Grade.txt
Awk-f ' # ' {print (index ($ $, "E")} ' Grade.txt
awk Escape character
Escape character
\b Backspace key
\ t Tab key
\f Paper Change page
\DDD octal value
\ n New Line
\c any other special character, such as \ \ is a backslash symbol
\ r Enter
Example
Awk-f ' # ' {print (index ($), "s"), "\ T", $ grade.txt} '
Awk-f ' # ' {print (index ($), "s"), "\ n", $ $ ' grade.txt
printf modifier, the printf function similar to the C language
printf modifier
%c ASCII character
%d integers
%f floating point numbers, e.g. (123.44)
%e floating-point number, scientific counting method
%f New Line
%g awk determines which floating-point number to use to convert E or F
%o Octal number
%s string
%x hexadecimal number
Example
Awk-f ' # ' {printf '%c\n ', ' $ ' grade.txt
Awk-f ' # ' {printf "%c\t%d\n", $1,$1} ' Grade.txt
Awk-f ' # ' {printf "%c\t%f\t%d\n", $1,$1,$1} ' Grade.txt
awk Array
Brief introduction
The awk array subscript starts with 1 instead of 0
Example
awk ' BEGIN {print split ("as#qw#1234", Array2, "#")} '
awk ' BEGIN {print split ("as#qw#1234", Array2, "#"); print array2[1]} '
awk ' BEGIN {print split ("as#qw#1234", Array2, "#"), print array2[1], "\ T", array2[2], "\ T", array2[3]} '
awk Script Example (awk_array.sh file)
Executes the command./awk_array.sh grade.txt

#!/bin/awk-fbegin{fs= "#" score["0-60"]=0 score["60-70"]=0 score["70-8         0 "]=0 score[" 80-90 "]=0 score[" 90-100 "]=0 student[" Junior "]=0 student[" Senior "]=0}{{if [$1<60]        score["0-60"]++} {if [$1<70] && [$1>=60] score["60-70"]++} {        If [$1<80] && [$1>=70] score["70-80"]++} {if [$1<90] && [$1>=80]  score["80-90"]++} {if [$1<=100] && [$1>=90] score["90-100"]++}}{for  Senior_junior in Student {if [$2==senior_junior] student[senior_junior]++}}end{for number             In score print "The Score", Number, "have", Score[number], "students"} {for Senior_junior in student Print "The class has", Student[senior_junior],senior_junior, "Stuents"}} 

1.2. In-depth discussion <<

Linux Shell Advanced Programming Tips 1---in-depth discussion (awk, <<)

Related Article

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.