[Linux Command Explanation Encyclopedia] 051. Field delimiters and process control in the Linux Awk scripting language

set field delimiter

The default field delimiter is space, you can specify a delimiter explicitly with -F "delimiter":

awk -F: '{ print $NF }' /etc/passwd
# 或
awk 'BEGIN{ FS=":" } { print $NF }' /etc/passwd

In the BEGIN statement block, OFS="delimiter" can be used to set the delimiter of the output field.

flow control statement

In the while, do-while and for statements of linux awk, break and continue statements are allowed to control the flow direction, and statements such as exit are also allowed to exit. break interrupts the currently executing loop and jumps outside the loop to execute the next statement. if is process selection usage. In awk, flow control statements, grammatical structures, and c language types. With these statements, many shell programs can be handed over to awk, and the performance is very fast. The following is the usage of each statement.

conditional statement

if(表达式)
  语句1
else
  语句2

Statement 1 in the format can be multiple statements. For the convenience of judgment and reading, it is better to enclose multiple statements with {}. The awk branch structure allows nesting, and its format is:

if(表达式)
  {
    
    语句1}
else if(表达式)
  {
    
    语句2}
else
  {
    
    语句3}

Example:

awk 'BEGIN{
test=100;
if(test>90){
  print "very good";
  }
  else if(test>60){
    print "good";
  }
  else{
    print "no pass";
  }
}'

output:

very good

Each command statement can be followed by a ; semicolon at the end.

loop statement

while statement

while(表达式)
  {
    
    语句}

Example:

awk 'BEGIN{
test=100;
total=0;
while(i<=test){
  total+=i;
  i++;
}
print total;
}'

output:

5050

for loop

There are two formats for for loops:

Format 1:

for(变量 in 数组)
  {
    
    语句}

Example:

awk 'BEGIN{
for(k in ENVIRON){
  print k"="ENVIRON[k];
}

}'

output:

TERM=linux
G_BROKEN_FILENAMES=1
SHLVL=1
pwd=/root/text
...
logname=root
HOME=/root
SSH_CLIENT=192.168.1.21 53087 22

Note: ENVIRON is an awk constant and is a subtypical array.

Format 2:

for(变量;条件;表达式)
  {
    
    语句}

Example:

awk 'BEGIN{
total=0;
for(i=0;i<=100;i++){
  total+=i;
}
print total;
}'

output:

5050

do loop

do
{
    
    语句} while(条件)

example:

awk 'BEGIN{ 
total=0;
i=0;
do {total+=i;i++;} while(i<=100)
  print total;
}'

output:

5050

other statements

  • break When used with a while or for statement, the break statement causes the program loop to exit.
  • continue When the continue statement is used with a while or for statement, causes the program loop to move to the next iteration.
  • next can cause the next input line to be read and return to the top of the script. This avoids performing additional operations on the current input line.
  • The exit statement causes the main input loop to exit and transfers control to END, if END exists. If no END rule is defined, or an exit statement is applied within END, execution of the script is terminated.

Array application

Arrays are the soul of awk, and the most important thing in processing text is its array processing. Because array indices (subscripts) can be numbers and strings, arrays in awk are called associative arrays. Arrays in awk do not have to be declared ahead of time, nor do they have to declare their size. Array elements are initialized with 0 or an empty string, depending on the context.

array definition

Numbers as array indices (subscripts):

Array[1]="sun"
Array[2]="kai"

String as array index (subscript):

Array["first"]="www"
Array"[last"]="name"
Array["birth"]="1987"

In use, print Array[1] will print sun; use print Array[2] will print kai; use print["birth"] will get 1987.

read the value of the array

{
    
     for(item in array) {
    
    print array[item]}; }       #输出的顺序是随机的
{
    
     for(i=1;i<=len;i++) {
    
    print array[i]}; }         #Len是数组的长度

Array related functions

Get the length of the array:

awk 'BEGIN{info="it is a test";lens=split(info,tA," ");print length(tA),lens;}'

output:

4 4

length returns the string and the length of the array, split splits the string into an array, and returns the length of the array obtained by splitting.

awk 'BEGIN{info="it is a test";split(info,tA," ");print asort(tA);}'

output:

4

asort sorts an array and returns the length of the array.

Output array content (unordered, ordered output):

awk 'BEGIN{info="it is a test";split(info,tA," ");for(k in tA){print k,tA[k];}}'

output:

4 test
1 it
2 is
3 a 

for...in output, because the array is an associative array, the default is unordered. So through for...in is an unordered array. If you need to get an ordered array, you need to get it by subscript.

awk 'BEGIN{info="it is a test";tlen=split(info,tA," ");for(k=1;k<=tlen;k++){print k,tA[k];}}'

output:

1 it
2 is
3 a
4 test

Note: The array subscript starts from 1, which is different from the C array.

Determine the existence of the key value and delete the key value:

Wrong judgment method:

awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";if(tB["c"]!="1"){print "no found";};for(k in tB){print k,tB[k];}}' 

output:

no found
a a1
b b1
c

There is a strange problem above, tB["c"] is not defined, but when looping, it is found that the key value already exists, and its value is empty. It should be noted here that the awk array is an associative array. As long as its key is referenced through the array, it will be The sequence will be created automatically.

The correct way to judge:

awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";if( "c" in tB){print "ok";};for(k in tB){print k,tB[k];}}'  

output:

a a1
b b1

if(key in array) uses this method to determine whether the key value is contained in the array.

Delete key value:

awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";delete tB["a"];for(k in tB){print k,tB[k];}}'                     

output:

b b1

delete array[key] can be deleted, corresponding to the sequence value of the array key.

Use of two-dimensional and multidimensional arrays

Awk's multidimensional array is essentially a one-dimensional array. More precisely, awk does not support multidimensional arrays in storage. Awk provides access methods that logically simulate two-dimensional arrays. For example, accesses like array[2,4]=1 are allowed. Awk uses a special string SUBSEP (�34) as a split field. In the above example, the key value stored in the associative array array is actually 2�344.

Similar to the membership test of a one-dimensional array, multidimensional arrays can use syntax such as if ( (i,j) in array), but the subscript must be placed in parentheses. Similar to the iterative access of one-dimensional arrays, multidimensional arrays use for (item in array) syntax to traverse the array. Unlike one-dimensional arrays, multidimensional arrays must use the split() function to access individual subscripted components.

awk 'BEGIN{
for(i=1;i<=9;i++){
  for(j=1;j<=9;j++){
    tarr[i,j]=i*j; print i,"*",j,"=",tarr[i,j];
  }
}
}'

output:

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6 
...
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81

The array content can be obtained by reference to array[k,k2].

Another way:

awk 'BEGIN{
for(i=1;i<=9;i++){
  for(j=1;j<=9;j++){
    tarr[i,j]=i*j;
  }
}
for(m in tarr){
  split(m,tarr2,SUBSEP); print tarr2[1],"*",tarr2[2],"=",tarr[m];
}
}'

Learn from scratchpython

[Learn python from zero] 92. Use Python's requests library to send HTTP requests and process responses
[Learn python from zero] 91. Use decorators and dictionaries to manage simple web applications for request paths
[Learn python from zero] 93. Use dictionary management Request path
[Learn python from zero] 89. Use WSGI to build a simple and efficient web server
[Learn python from zero] 88. Detailed explanation of WSGI interface: realize simple and efficient web development
[Learn python from zero] 87. Manually build HTTP server Python Implementation and multi-threaded concurrent processing
[Learn python from zero] 86. In-depth understanding of HTTP protocol and its role in browser and server communication
[Learn python from zero] 85. Parallel computing technology application of Python process pool
[Learn python from zero ] 84. In-depth understanding of threads and processes
[learning python from zero] 83. Python multi-process programming and the use of process pools
[learning python from zero] 82. Realization of chat programs based on multi-threading
[learning python from zero] 81. Python and more Application of thread communication and queue
[learning python from zero] 80. Thread access to global variables and thread safety issues
[learning python from zero] 79. Thread access to global variables and thread security issues
[learning python from zero] 78. File download cases
[ Learning python from zero] 77. TCP server programming and precautions
[learning python from zero] 76. Server and client: key components of network communication
[learning python from zero] 75. TCP protocol: reliable connection-oriented transmission layer communication protocol
[Learn python from zero] 74. UDP network program: detailed explanation of port problems and binding information
[Learn python from zero] 73. UDP network program - send data
[Learn python from zero] 72. In-depth understanding of Socket communication and socket creation The method of
[learning python from zero] 71. Network ports and their functions
[learning python from zero] 70. Network communication methods and their applications: from direct communication to routers to connect multiple networks
[learning python from zero] 69. Network communication and IP address classification analysis
[learning python from zero] 68. Greedy and non-greedy modes in Python regular expressions
[learning python from zero] 67. re module in Python: regular replacement and advanced matching technology
[learning python from zero] 66 .In-depth understanding of regular expressions: a powerful tool for pattern matching and text processing
[Learn python from zero] 65. Detailed explanation of Python regular expression modifiers and their applications
[Learn python from zero] 64. The re.compile method in Python regular expressions Detailed explanation
[learning python from zero] 63. Introduction to the re.Match class in regular expressions and its attributes and methods
[learning python from zero] 62. Python regular expressions: a powerful string matching tool
[learning python from zero] 61. Detailed explanation and application examples of property attributes in Python
[learning python from zero] 60. Exploration generator: a flexible tool for iteration
[learning python from zero] 59. Iterator: an efficient tool for optimizing data traversal
[learning python from zero] 58. Custom exceptions in Python and methods of raising exceptions
[Learn python from zero] 57. Use the with keyword in Python to correctly close resources
[Learn python from zero] 56. The importance and application of exception handling in programming
[Learn python from zero] 55. Serialization and processing in Python Deserialization, application of JSON and pickle modules
[learning python from zero] 54. Writing data in memory
[learning python from zero] 53. CSV files and Python CSV modules
[learning python from zero] 52. Reading and writing files - Python file operation guide
[learning python from zero] 51. Opening and closing of files and its application in Python
[learning python from zero] 49. Object-related built-in functions in Python and their usage
[learning python from zero] 48 .Detailed explanation of inheritance and multiple inheritance in Python
[learning python from zero] 47. The concept and basic use of inheritance in object-oriented programming
[learning python from zero] 46. __new__ and __init__ method analysis and singleton in Python Design patterns
[learning python from zero] 45. Class methods and static methods in Python
[learning python from zero] 44. Private attributes and methods in object-oriented programming
[learning python from zero] 43. Examples in Python object-oriented programming Attributes and class attributes
[Learn python from zero] 42. Built-in attributes and methods in Python
[Learn python from zero] 41. Python magic method (2)
[Learn python from zero] 40. Python magic method (1)
[Learn python from zero] 39. Object-oriented basic grammar and application examples
[Learn python from zero] 38. Use and import of Python packages
[Learn python from zero] 37. Use and precautions of Python custom modules
[From zero Learning python] 36. Methods and skills of using pip in Python for third-party package management
[learning python from zero] 35. Python common system modules and their usage
[learning python from zero] 34. Detailed explanation of the import and use of Python modules
[ Learn python from zero] 33. The role of decorators (2)
[Learn python from zero] 32. The role of decorators (1)
[Learn python from zero] 31. In-depth understanding of higher-order functions and closures in Python
[from Zero learning python] 30. In-depth understanding of recursive functions and anonymous functions
[learning python from zero] 29. "Detailed explanation of function parameters" - understand the different usages of Python function parameters
[learning python from zero] 28. Local variables and globals in Python Variables
[Learn python from zero] 27. Use and nested calls of Python functions
[Learn python from zero] 25. Functions: a tool to improve code writing efficiency
[Learn python from zero] 24. String operations and traversal methods in Python
[Learn python from zero] 23. How to use and common operations of sets (set) in Python
[Learn python from zero] 22. Addition, deletion, modification and query of dictionaries in Python and variables of dictionaries
[Learn python from zero] 21. In Python tuples and dictionaries for
[Learn python from zero] 20. Python list operation skills and examples
[Learn python from zero] 19. Application of looping through lists and list nesting
[Learn python from zero] 18. Detailed explanation of basic operations of Python lists (1)
[From Zero learning python] 17. Format method of Python string (2)
[Learn python from zero] 16. Format method of Python string (1)
[Learn python from zero] 15. In-depth understanding of string and character set encoding
[from Zero learning python] 14. Common operations of Python strings (2)
[Learn python from zero] 13. Common operations of Python strings (1)
[Learn python from zero] 12. Python string operations and applications
[Learn python from zero] 11.Python loop statement and control flow
[learn python from zero] 10. Detailed explanation of Python conditional statement and if nesting
[learn python from zero] 09. Conditional judgment statement in Python
[learn python from zero] 08. Python understands bit operations Operators, operator priority
[learn python from zero] 07. Detailed explanation of Python operators: assignment, comparison and logical operators
[learn python from zero] 06. Use arithmetic operators in Python for calculation and string concatenation
[learn from zero python] 05. Output and input in Python
[learn python from zero] 04. Basics of Python programming: variables, data types and identifiers
[learn python from zero] 03. Python interactive programming and detailed annotations
[Learn python from zero] 02. Introduction to development tools
[Learn python from zero] 01. Install and configure python

Guess you like

Origin blog.csdn.net/qq_33681891/article/details/132661958