A little test of operation and maintenance shell script (12): try out awk programming


A little test of operation and maintenance shell script (1)

A little test of operation and maintenance shell script (2)

A quick test of operation and maintenance Shell script (3)::$(cd $(dirname $0); pwd) command detailed explanation

A little test of operation and maintenance Shell script (4): multi-layer nesting if...elif...elif...else fi_Snail Yang Ge's Blog-CSDN Blog

Cenos7 installation train program animation

A little test of the operation and maintenance shell script (5): until loop

A little test of operation and maintenance Shell script (6): Function recognition in Shell

A little test of operation and maintenance Shell script (7): Call a function in another script file from a function file

A little trial of operation and maintenance shell script (8): case mode ignores the case mode of command line parameters demonstration

Operation and maintenance Shell script master test (9): redirection operator ">" and double redirection ">>"

 A little trial of operation and maintenance shell script (10)

Operation and maintenance shell test (11): for loop reads multiple command line parameters

Try out the operation and maintenance shell script: try out awk programming


 1: Introduction to awk

       Awk is an interpreted programming language designed for text processing and commonly used as a data extraction and reporting tool. It is mainly used for a variety of computing and data processing tasks; awk provides a more general computing model for processing files. A typical application example of the awk program is to convert data into formatted reports. Usually these data may be logs generated by Linux programs, and the formatted reports are a useful format for system administrators to summarize the data;

      Usually awk has the following functions

         1) Use variables to operate text files composed of text records and fields

         2) Has arithmetic and string operators

         3) Have common programming structures. Such as loops and conditions

         4) Generate formatted reports

         5) Define functions

         6) Execute Linux commands from awk script

         7) Processing the results of Linux commands

         8) Handle command line parameters more skillfully

         9) Easier processing of multiple input streams


2: Trial on basic usage scenarios


Prepare info.txt text file

[root@www awk]# cat info.txt 
Linux - Sysadmin
Databases - Oracle, Mysql etc.
Security - Firewall, Network, Online Security etc.
Cool - Websites
Storage - NetApp, EMC etc.
Productivity - Too Many technologies to explore, no much time available
     


2.1: awk displays text file content

[root@www awk]# awk '{ print }' info.txt 
Linux - Sysadmin
Databases - Oracle, Mysql etc.
Security - Firewall, Network, Online Security etc.
Cool - Websites
Storage - NetApp, EMC etc.
Productivity - Too Many technologies to explore, no much time available
2.2 Match records with file delimiter '-'

[root@www awk]#  awk -F'-' '{ print $0 }' info.txt 
Linux - Sysadmin
Databases - Oracle, Mysql etc.
Security - Firewall, Network, Online Security etc.
Cool - Websites
Storage - NetApp, EMC etc.
Productivity - Too Many technologies to explore, no much time available
[root@www awk]#  awk -F'-' '{ print $1 }' info.txt 
Linux 
Databases 
Security 
Cool 
Storage 
Productivity 
[root@www awk]#  awk -F'-' '{ print $2 }' info.txt 
 Sysadmin
 Oracle, Mysql etc.
 Firewall, Network, Online Security etc.
 Websites
 NetApp, EMC etc.
 Too Many technologies to explore, no much time available
[root@www awk]#  awk -F'-' '{ print $3 }' info.txt 

2.3: Match records using regular expressions 

As follows, use "-" as the delimiter to match records containing "etc"


[root@www awk]# awk 'BEGIN { FS="-" } $2 ~ "etc" { print $1 }' info.txt 
Databases 
Security 
Storage 
[root@www awk]# awk 'BEGIN { FS="-" } $2 ~ "etc" { print $0 }' info.txt 
Databases - Oracle, Mysql etc.
Security - Firewall, Network, Online Security etc.
Storage - NetApp, EMC etc.
[root@www awk]# awk 'BEGIN { FS="-" } $2 ~ "etc" { print $2 }' info.txt 
 Oracle, Mysql etc.
 Firewall, Network, Online Security etc.
 NetApp, EMC etc.
 

Three: Read awk commands from awk scripts


[root@www awk]# cat secondeAwk.awk 
BEGIN {
 FS="-"
 
}
{ print $2 }


Execute the script:

[root@www awk]# awk -f secondeAwk.awk info.txt 
 Sysadmin
 Oracle, Mysql etc.
 Firewall, Network, Online Security etc.
 Websites
 NetApp, EMC etc.
 Too Many technologies to explore, no much time available
 

Guess you like

Origin blog.csdn.net/u014635374/article/details/132941712