awk

xiaoxiao2021-03-05  23

(Author: Emmett Dulaney)

The AWK utility has its own self-contained language, not only in Linux is also one of the most powerful data processing engines in any environment. The biggest feature of this programming and data operation language (its name to its founder ALFRED AHO, Peter Weinberger and Brian Kernighan last name) depends on the knowledge you have. It allows you to create short programs that read input files, sorted by sorting, processing data, perform calculations for input, and generate reports, and countless functions. What is AWK? Saturm, AWK is a programming language tool for processing text. The language of the AWK utility is similar to the shell programming language, although AWK has a syntax that is completely its own. When you originally created AWK, its purpose is to use for text processing, and the basis of this language is that a series of instructions are performed as long as there is a mode match in the input data. This utility scans each row in the file to find the mode that matches the content given in the command line. If you find the matching content, the next programming step is performed. If you can't find the match, continue to process the next line. Although the operation may be complicated, the syntax of the command is always: awk '{pattern action}' {filenames} where patter represents the content that awk looks up in the data, and action is a series of commands performed when the match is found. . Praggies ({}) do not need to appear in the program, but they are used to group a series of instructions based on a particular mode. The Field Utility is divided into records and fields for each input line. Record is a single-line input, and each record contains several fields. The default field separator is a space or a tab, and the recorded separator is a wrap. While watching tabs and spaces are viewed by default (multiple spaces still as a separator), the separator can be changed from space to any other characters. To make a demonstration, check the staff list file saved as EMP_NAMES:

46012 Dulaney Evan Mobile Al

46013 Durham Jeff Mobile Al

46015 Steen Bill Mobile Al

46017 Feldman Evan Mobile Al

46018 Swim Steve Unknown Al

46019 Bogue Robert Phoenix AZ

46021 June Micah Phoenix AZ

46022 Kane Sheryl Unknown Ar

46024 Wood William Muncie in

46026 Fergus Sarah Muncie in

46027 Buck Sarah Muncie in

46029 TUTTLE BOB MUNCIE IN

When the AWK reads the input content, the entire record is assigned to the variable $ 0. Each field is separated by field separator, assigned to a variable $ 1, $ 2, $ 3, and more. One line can contain countless fields in nature, access each field through the field number. Therefore, command

Awk '{Print $ 1, $ 2, $ 3, $ 4, $ 5}' Names

The print output will result

46012 Dulaney Evan Mobile Al

46013 DURHAM JEFF Mobile Al46015 Steen Bill Mobile Al

46017 Feldman Evan Mobile Al

46018 Swim Steve Unknown Al

46019 Bogue Robert Phoenix AZ

46021 June Micah Phoenix AZ

46022 Kane Sheryl Unknown Ar

46024 Wood William Muncie in

46026 Fergus Sarah Muncie in

46027 Buck Sarah Muncie in

46029 TUTTLE BOB MUNCIE IN

An important thing that is worth noting is that the AWK interprets five fields separated by spaces, but when it prints the display, there is only one space between each field. With the function of specifying a unique number for each field, you can select only a specific field. For example, only the name of each record is printed, just select the second and third fields:

$ awk '{Print $ 2, $ 3}' EMP_NAMES

Dulaney Evan

DURHAM JEFF

Steen Bill

Feldman Evan

Swim Steve

Bogue Robert

June Micah

Kane Sheryl

Wood William

Fergus sarah

Buck Sarah

TUTTLE BOB

$

You can also specify print fields in any order, regardless of how they exist in the record. Therefore, only the name field is required, and the order is reversed, first display the name and then display the last name:

$ awk '{Print $ 3, $ 2}' EMP_NAMES

Evan Dulaney

Jeff durham

Bill Steen

Evan Feldman

Steve Swim

Robert Bogue

Micah June

Sheryl Kane

William Wood

Sarah Fergus

Sarah Buck

Bob Tuttle

$

Adding text can be added to the display in the same way as adding control sequences or other characters. For example, to change the separator from the space to a colon, the command is

AWK '{Print $ 1: "$ 2": "$ 3": "$ 4": "$ 5}' EMP_NAMES> New_EMP_NAMES

In this case, the character (:) is included in the quotation mark ("/"), which is added to each field. The value between quotation marks can be anything. For example, create a look similar to the display of employees living in Alabama:

$ awk '$ 5 ~ / al / {print "Name:" $ 2 "," $ 3 "/ ncity-state:

"$ 4", "$ 5" / n "} 'EMP_NAMES

Name: Dulaney, Evan

City-state: Mobile, Al

Name: durham, jeff

City-state: Mobile, Al

Name: Steen, Bill

City-state: Mobile, Al

Name: Feldman, Evan

City-state: Mobile, Al

Name: SWIM, Steve

City-state: unknown, al

$

Mathematical operation

In addition to providing text functions, AWK also provides full range of arithmetic operators, including the following symbols:

Add numbers

- Less

* Multiplied

/ Division

^ Execute Index Operation

% Provide a model

plus the variable value

= Assign the results of other operations to the variable

- minus variables

- = Assign the result of subtraction operation to variables

* = Results of allocation multiplication

/ = Result of distributing division operation

% = Allocate Sample Operation Results

For example, assume that there is the following files on your machine, list the items in the hardware store in detail:

$ Cat Inventory

Hammers 5 7.99

DRILLS 2 29.99

Punches 7 3.59

Drifts 2 4.09

BITS 55 1.19

Saws 123 14.99

Nails 800.19

Screws 80.29

BRADS 100 .24

$

The first business order is by multiplying the value of the second field (number) by the value of the third field (price), calculates the inventory value of each item:

$ awk '{Print $ 1, "Qty:" $ 2, "Price:" $ 3, "Total:" $ 2 * $ 3}' Inventory

Hammers Qty: 5 Price: 7.99 Total: 39.95

DRILLS Qty: 2 Price: 29.99 Total: 59.98

Punches QTY: 7 Price: 3.59 Total: 25.13

Drifts Qty: 2 Price: 4.09 Total: 8.18

BITS Qty: 55 Price: 1.19 Total: 65.45

Saws Qty: 123 Price: 14.99 Total: 1843.77

Nails Qty: 800 Price: .19 Total: 152

Screws Qty: 80 Price: .29 Total: 23.2

BRADS Qty: 100 Price: .24 Total: 24

$

If these lines itself is not important, you just want to determine how many items in the store, you can assign a normal variable, increase the number of items in each record:

$ awk '{x = x $ 2} {print x}' inventory

5

Seduce

14

16

71

194

994

1074

1174

$

According to this data, there are 1174 items in the store. When executed for the first time, the variable X does not value, so it uses the value of the first line second field. When executed for the second execution, it retains the value of the first row and adds the value of the second line, so that the cumulative total is reached.

The same process can be applied to determine the total value of existing stocks:

$ awk '{x = x ($ 2 * $ 3)} {print x}' inventory

39.95

99.93

125.06

133.24

198.69

2042.46

2194.46

2217.66

2241.66

$

Therefore, the value of 1174 items is $ 2,241.66. Although this process can obtain a total value, it is poor and needs to be processed into actual reports. Using some additional items, it is easy to make the display more neat:

$ awk '{x = x ($ 2 * $ 3)} {Print $ 1, "Qty:" $ 2, "Price:" $ 3, "Total:" $ 2 * $ 3, "BAL:" x}' Inventory

Hammers Qty: 5 Price: 7.99 Total: 39.95 Bal: 39.95Drills Qty: 2 Price: 29.99 Total: 59.98 BAL: 99.93

Punches QTY: 7 Price: 3.59 Total: 25.13 Bal: 125.06

Drifts Qty: 2 Price: 4.09 Total: 8.18 Bal: 133.24

Bits Qty: 55 Price: 1.19 Total: 65.45 Bal: 198.69

Saws Qty: 123 Price: 14.99 Total: 1843.77 Bal: 2042.46

Nails Qty: 800 Price: .19 Total: 152 Bal: 2194.46

Screws Qty: 80 Price: .29 Total: 23.2 Bal: 2217.66

BRADS Qty: 100 Price: .24 Total: 24 Bal: 2241.66

$

This process provides a list of each record while assigning the total value to the inventory value, and maintaining the operation balance of the store asset.

Begin and End

Use the begin and end statements to specify separately before or after processing actual start. The Begin statement is most commonly used to establish a variable or display title. On the other hand, the END statement can be used to continue processing after the end of the program.

In the previous example, the total value of the item is generated using the following routines:

awk '{x = x ($ 2 * $ 3)} {print x}' inventory

This routine shows each row in the file when running total. There is no other way to specify it without making printing in each row, causing it to never print. However, using the END statement to avoid this question:

$ awk '{x = x ($ 2 * $ 3)} end {print "Total value of inventory:" x}' inventory

Total Value of Inventory: 2241.66

$

Variable X is defined, it processes each line; however, no display is generated before all processing is complete. Although it can be used as a stand-alone routine, it can also be placed to the previous code list, add more information and generate more complete reports:

$ awk '{x = x ($ 2 * $ 3)} {Print $ 1, "Qty:" $ 2, "Price:

"$ 3," Total: "$ 2 * $ 3} End {print" Total Value of Inventory: "x} 'inventory

Hammers Qty: 5 Price: 7.99 Total: 39.95

DRILLS Qty: 2 Price: 29.99 Total: 59.98

Punches QTY: 7 Price: 3.59 Total: 25.13

Drifts Qty: 2 Price: 4.09 Total: 8.18

BITS Qty: 55 Price: 1.19 Total: 65.45

Saws Qty: 123 Price: 14.99 Total: 1843.77

Nails Qty: 800 Price: .19 Total: 152

Screws Qty: 80 Price: .29 Total: 23.2

BRADS Qty: 100 Price: .24 Total: 24

Total Value of Inventory: 2241.66

$

The begin command is the same as the work mode of End, but it has established items that need to be made before completing other work. The most common purpose of this process is to create a header of the report. The syntax of this routine is similar to

$ awk 'begin {print "Item Quantity Price Total"}' Input, Output, and Source File

The awk tool can read its input from the file. As you do before this, it can also get input from the output of other commands. E.g:

$ sort emp_names | awk '{Print $ 3, $ 2}'

The input of the awk command is the output of the sort operation. In addition to sort, you can also use any other Linux commands - such as GREP. This process allows you to perform other operations on files before leaving the selected field.

Similar to the interpreter, the AWK uses the output and modirective operators> and >> put it in the file instead of the standard output device. The effects of these symbols are similar to their corresponding symbols in the interpreter, so> Create a file when there is no file, and >> Add to the tail of the existing file. Please see the following example:

$ awk '{print nr, $ 1)> "/ tmp / filez"}' EMP_NAMES

$ CAT / TMP / FILEZ

1 46012

2 46013

3 46015

4 46017

5 46018

6 46019

7 46021

8 46022

9 46024

10 46026

11 46027

12 46029

$

Check the syntax of this statement, you will see the output reigning is done after the print statement is completed. The file name must be included in the quotation marks, otherwise it is just an unmetuted AWK variable, and the instruction is coupled to the AWK. (If you use the re-symbol correctly, AWK can't understand that the symbol means "re-direction" or a relational operator.)

Output to the pipe in the AWK is similar to the same operations implemented in the interpreter. To send the output of the print command to the pipe, you can add the piping symbol and the name of the command after printing the command, as shown below:

$ awk '{print $ 2 | "sort"}' EMP_NAMES

Bogue

Buck

Dulaney

DURHAM

Feldman

Fergus

June

Kane

Steen

SWIM

TUTTLE

Wood

$

This is the case where the output is changed, and the command must be included in the quotation mark, and the name of the pipe is the name of the executed command.

The command used by awk can come from two places. First, you can specify them in the command line, as shown in the example. Second, they can be provided by the source file. If this is the case, this situation will be warned to the AWK through the -f option. The demo is as follows:

$ cat awklist

{Print $ 3, $ 2}

{Print $ 4, $ 5, "/ N"}

$

$ awk -f awklist EMP_NAMES

Evan Dulaney

Mobile al

Jeff durham

Mobile al

Bill Steen

Mobile al

Evan Feldman

Mobile al

Steve Swim

Unknown Al

Robert Bogue

Phoenix az

Micah June

Phoenix az

Sheryl Kane

Unknown Ar

William Wood

Muncie in

Sarah Fergus

Muncie in

Sarah Buck

Muncie in

Bob Tuttle

Muncie in

$

Note that no single quotes are used anywhere in the source file or when you call it in the command line. Single quotes are only used to distinguish commands and file names in the command line.

If a simple output cannot handle the complex information you need in your program, you can attempt to be more complex output by the printf command, its syntax is Printf (Format, Value, Value ...)

This syntax is similar to the Printf command in the C language, and the specifications of the format are the same. This format can be defined by inserting a definition of how to print a value. The format specification contains a% of the letter with letters. Similar to the print command, Printf does not have to be included in parentheses, but it can be considered to use parentheses to be a good habit.

The following table lists the various specifications provided by the printf command.

Specifications

% C prints a single ASCII character

% D print decimal

% e print digital scientific count

% F print floating point

% g Print% e or% f; two ways are shorter

% o Print unsigned octal number

S prints a ASCII string

% x Print unsigned hexadecimal number

%% printing percent sign; do not perform conversion

Some additional formatting parameters can be provided between% and characters. These parameters further improve the value of the value:

Parameter Description

- Align the expression in the field

Width is supplemented to the specified width as needed (the preamble zero uses zero to make fields)

.prec decimal point on the maximum string width or maximum number of numbers

The Printf command can control and convert the value from one format to another. When the value of the print variable is required, simply provide a specification, indicating how the printf prints information (usually included in double quotes). Variables that must be passed to PrINTF include a specification parameter; if there is a small parameter, the Printf does not print all values.

Handling error

The way the AWK tool report has occurred is annoyed. An error will hinder any operation, the error message provided is very mixed:

awk: Syntax Error Near Line 2

awk: bailing out ner line 2

You may take a few hours to view the second line, trying to find out why it hinders the program to run; this is a powerful argument that supports the use of the source file.

Remember that two rules can help you avoid syntax errors:

1. Make sure the command is in parentheses, and parentheses is in single quotes. No one of these characters must cause the program to fail.

2. The search command needs to be between the slash. To find out the employee living in Indiana, you must use "/ in /" instead of "in".

in conclusion

Although AWK completely represents additional meaning, it should be the initial abbreviation of the administrator smart toolkit. With SED, the AWK utility is one of the most powerful and flexible tools owned by Linux administrators. By understanding some of their language, you can open up a field that simplifies tasks, otherwise these tasks will be very time and difficult.

46012 Dulaney Evan Mobile Al

46013 Durham Jeff Mobile Al

46015 Steen Bill Mobile Al

46017 Feldman Evan Mobile Al

46018 Swim Steve Unknown Al

46019 Bogue Robert Phoenix AZ

46021 June Micah Phoenix AZ

46022 Kane Sheryl Unknown Ar

46024 Wood William Muncie in

46026 Fergus Sarah Muncie in

46027 Buck Sarah Muncie In46029 Tuttle Bob Muncie in

When the AWK reads the input content, the entire record is assigned to the variable $ 0. Each field is separated by field separator, assigned to a variable $ 1, $ 2, $ 3, and more. One line can contain countless fields in nature, access each field through the field number. Therefore, command

Awk '{Print $ 1, $ 2, $ 3, $ 4, $ 5}' Names

The print output will result

46012 Dulaney Evan Mobile Al

46013 Durham Jeff Mobile Al

46015 Steen Bill Mobile Al

46017 Feldman Evan Mobile Al

46018 Swim Steve Unknown Al

46019 Bogue Robert Phoenix AZ

46021 June Micah Phoenix AZ

46022 Kane Sheryl Unknown Ar

46024 Wood William Muncie in

46026 Fergus Sarah Muncie in

46027 Buck Sarah Muncie in

46029 TUTTLE BOB MUNCIE IN

An important thing that is worth noting is that the AWK interprets five fields separated by spaces, but when it prints the display, there is only one space between each field. With the function of specifying a unique number for each field, you can select only a specific field. For example, only the name of each record is printed, just select the second and third fields:

$ awk '{Print $ 2, $ 3}' EMP_NAMES

Dulaney Evan

DURHAM JEFF

Steen Bill

Feldman Evan

Swim Steve

Bogue Robert

June Micah

Kane Sheryl

Wood William

Fergus sarah

Buck Sarah

TUTTLE BOB

$

You can also specify print fields in any order, regardless of how they exist in the record. Therefore, only the name field is required, and the order is reversed, first display the name and then display the last name:

$ awk '{Print $ 3, $ 2}' EMP_NAMES

Evan Dulaney

Jeff durham

Bill Steen

Evan Feldman

Steve Swim

Robert Bogue

Micah June

Sheryl Kane

William Wood

Sarah Fergus

Sarah Buck

Bob Tuttle

$

Adding text can be added to the display in the same way as adding control sequences or other characters. For example, to change the separator from the space to a colon, the command is

AWK '{Print $ 1: "$ 2": "$ 3": "$ 4": "$ 5}' EMP_NAMES> New_EMP_NAMES

In this case, the character (:) is included in the quotation mark ("/"), which is added to each field. The value between quotation marks can be anything. For example, create a look similar to the display of employees living in Alabama:

$ awk '$ 5 ~ / al / {print "Name:" $ 2 "," $ 3 "/ ncity-state:

"$ 4", "$ 5" / n "} 'EMP_NAMES

Name: Dulaney, Evan

City-state: mobile, alname: durham, jeff

City-state: Mobile, Al

Name: Steen, Bill

City-state: Mobile, Al

Name: Feldman, Evan

City-state: Mobile, Al

Name: SWIM, Steve

City-state: unknown, al

$

Mathematical operation

In addition to providing text functions, AWK also provides full range of arithmetic operators, including the following symbols:

Add numbers

- Less

* Multiplied

/ Division

^ Execute Index Operation

% Provide a model

plus the variable value

= Assign the results of other operations to the variable

- minus variables

- = Assign the result of subtraction operation to variables

* = Results of allocation multiplication

/ = Result of distributing division operation

% = Allocate Sample Operation Results

For example, assume that there is the following files on your machine, list the items in the hardware store in detail:

$ Cat Inventory

Hammers 5 7.99

DRILLS 2 29.99

Punches 7 3.59

Drifts 2 4.09

BITS 55 1.19

Saws 123 14.99

Nails 800.19

Screws 80.29

BRADS 100 .24

$

The first business order is by multiplying the value of the second field (number) by the value of the third field (price), calculates the inventory value of each item:

$ awk '{Print $ 1, "Qty:" $ 2, "Price:" $ 3, "Total:" $ 2 * $ 3}' Inventory

Hammers Qty: 5 Price: 7.99 Total: 39.95

DRILLS Qty: 2 Price: 29.99 Total: 59.98

Punches QTY: 7 Price: 3.59 Total: 25.13

Drifts Qty: 2 Price: 4.09 Total: 8.18

BITS Qty: 55 Price: 1.19 Total: 65.45

Saws Qty: 123 Price: 14.99 Total: 1843.77

Nails Qty: 800 Price: .19 Total: 152

Screws Qty: 80 Price: .29 Total: 23.2

BRADS Qty: 100 Price: .24 Total: 24

$

If these lines itself is not important, you just want to determine how many items in the store, you can assign a normal variable, increase the number of items in each record:

$ awk '{x = x $ 2} {print x}' inventory

5

Seduce

14

16

71

194

994

1074

1174

$

According to this data, there are 1174 items in the store. When executed for the first time, the variable X does not value, so it uses the value of the first line second field. When executed for the second execution, it retains the value of the first row and adds the value of the second line, so that the cumulative total is reached.

The same process can be applied to determine the total value of existing stocks:

$ awk '{x = x ($ 2 * $ 3)} {print x}' inventory

39.95

99.93

125.06

133.24

198.69

2042.46

2194.46

2217.66

2241.66

$

Therefore, the value of 1174 items is $ 2,241.66. Although this process can obtain a total value, it is poor and needs to be processed into actual reports. Using some additional items, it is easy to make the display more neat:

$ awk '{x = x ($ 2 * $ 3)} {Print $ 1, "Qty:" $ 2, "Price:" $ 3, "Total:" $ 2 * $ 3, "BAL:" x}' Inventory

Hammers Qty: 5 Price: 7.99 Total: 39.95 Bal: 39.95

Drills Qty: 2 Price: 29.99 Total: 59.98 Bal: 99.93

Punches QTY: 7 Price: 3.59 Total: 25.13 Bal: 125.06

Drifts Qty: 2 Price: 4.09 Total: 8.18 Bal: 133.24

Bits Qty: 55 Price: 1.19 Total: 65.45 Bal: 198.69

Saws Qty: 123 Price: 14.99 Total: 1843.77 Bal: 2042.46

Nails Qty: 800 Price: .19 Total: 152 Bal: 2194.46

Screws Qty: 80 Price: .29 Total: 23.2 Bal: 2217.66

BRADS Qty: 100 Price: .24 Total: 24 Bal: 2241.66

$

This process provides a list of each record while assigning the total value to the inventory value, and maintaining the operation balance of the store asset.

Begin and End

Use the begin and end statements to specify separately before or after processing actual start. The Begin statement is most commonly used to establish a variable or display title. On the other hand, the END statement can be used to continue processing after the end of the program.

In the previous example, the total value of the item is generated using the following routines:

awk '{x = x ($ 2 * $ 3)} {print x}' inventory

This routine shows each row in the file when running total. There is no other way to specify it without making printing in each row, causing it to never print. However, using the END statement to avoid this question:

$ awk '{x = x ($ 2 * $ 3)} end {print "Total value of inventory:" x}' inventory

Total Value of Inventory: 2241.66

$

Variable X is defined, it processes each line; however, no display is generated before all processing is complete. Although it can be used as a stand-alone routine, it can also be placed to the previous code list, add more information and generate more complete reports:

$ awk '{x = x ($ 2 * $ 3)} {Print $ 1, "Qty:" $ 2, "Price:

"$ 3," Total: "$ 2 * $ 3} End {print" Total Value of Inventory: "x} 'inventory

Hammers Qty: 5 Price: 7.99 Total: 39.95

DRILLS Qty: 2 Price: 29.99 Total: 59.98

Punches QTY: 7 Price: 3.59 Total: 25.13

Drifts Qty: 2 Price: 4.09 Total: 8.18

BITS Qty: 55 Price: 1.19 Total: 65.45

Saws Qty: 123 Price: 14.99 Total: 1843.77nails Qty: 800 Price: .19 Total: 152

Screws Qty: 80 Price: .29 Total: 23.2

BRADS Qty: 100 Price: .24 Total: 24

Total Value of Inventory: 2241.66

$

The begin command is the same as the work mode of End, but it has established items that need to be made before completing other work. The most common purpose of this process is to create a header of the report. The syntax of this routine is similar to

$ awk 'begin {print "item quantity price total"}'

Enter, output, and source files

The awk tool can read its input from the file. As you do before this, it can also get input from the output of other commands. E.g:

$ sort emp_names | awk '{Print $ 3, $ 2}'

The input of the awk command is the output of the sort operation. In addition to sort, you can also use any other Linux commands - such as GREP. This process allows you to perform other operations on files before leaving the selected field.

Similar to the interpreter, the AWK uses the output and modirective operators> and >> put it in the file instead of the standard output device. The effects of these symbols are similar to their corresponding symbols in the interpreter, so> Create a file when there is no file, and >> Add to the tail of the existing file. Please see the following example:

$ awk '{print nr, $ 1)> "/ tmp / filez"}' EMP_NAMES

$ CAT / TMP / FILEZ

1 46012

2 46013

3 46015

4 46017

5 46018

6 46019

7 46021

8 46022

9 46024

10 46026

11 46027

12 46029

$

Check the syntax of this statement, you will see the output reigning is done after the print statement is completed. The file name must be included in the quotation marks, otherwise it is just an unmetuted AWK variable, and the instruction is coupled to the AWK. (If you use the re-symbol correctly, AWK can't understand that the symbol means "re-direction" or a relational operator.)

Output to the pipe in the AWK is similar to the same operations implemented in the interpreter. To send the output of the print command to the pipe, you can add the piping symbol and the name of the command after printing the command, as shown below:

$ awk '{print $ 2 | "sort"}' EMP_NAMES

Bogue

Buck

Dulaney

DURHAM

Feldman

Fergus

June

Kane

Steen

SWIM

TUTTLE

Wood

$

This is the case where the output is changed, and the command must be included in the quotation mark, and the name of the pipe is the name of the executed command.

The command used by awk can come from two places. First, you can specify them in the command line, as shown in the example. Second, they can be provided by the source file. If this is the case, this situation will be warned to the AWK through the -f option. The demo is as follows:

$ cat awklist

{Print $ 3, $ 2}

{Print $ 4, $ 5, "/ N"}

$

$ awk -f awklist EMP_NAMES

Evan Dulaney

Mobile al

Jeff durham

Mobile al

Bill Steen

Mobile al

Evan Feldman

Mobile al

Steve Swim

Unknown Al

Robert Boguephoenix AZ

Micah June

Phoenix az

Sheryl Kane

Unknown Ar

William Wood

Muncie in

Sarah Fergus

Muncie in

Sarah Buck

Muncie in

Bob Tuttle

Muncie in

$

Note that no single quotes are used anywhere in the source file or when you call it in the command line. Single quotes are only used to distinguish commands and file names in the command line.

If a simple output cannot handle the complex information you need in your program, you can try the more complex output obtained by the printf command, its syntax is

Printf (Format, Value, Value ...)

This syntax is similar to the Printf command in the C language, and the specifications of the format are the same. This format can be defined by inserting a definition of how to print a value. The format specification contains a% of the letter with letters. Similar to the print command, Printf does not have to be included in parentheses, but it can be considered to use parentheses to be a good habit.

The following table lists the various specifications provided by the printf command.

Specifications

% C prints a single ASCII character

% D print decimal

% e print digital scientific count

% F print floating point

% g Print% e or% f; two ways are shorter

% o Print unsigned octal number

S prints a ASCII string

% x Print unsigned hexadecimal number

%% printing percent sign; do not perform conversion

Some additional formatting parameters can be provided between% and characters. These parameters further improve the value of the value:

Parameter Description

- Align the expression in the field

Width is supplemented to the specified width as needed (the preamble zero uses zero to make fields)

.prec decimal point on the maximum string width or maximum number of numbers

The Printf command can control and convert the value from one format to another. When the value of the print variable is required, simply provide a specification, indicating how the printf prints information (usually included in double quotes). Variables that must be passed to PrINTF include a specification parameter; if there is a small parameter, the Printf does not print all values.

Handling error

The way the AWK tool report has occurred is annoyed. An error will hinder any operation, the error message provided is very mixed:

awk: Syntax Error Near Line 2

awk: bailing out ner line 2

You may take a few hours to view the second line, trying to find out why it hinders the program to run; this is a powerful argument that supports the use of the source file.

Remember that two rules can help you avoid syntax errors:

1. Make sure the command is in parentheses, and parentheses is in single quotes. No one of these characters must cause the program to fail.

2. The search command needs to be between the slash. To find out the employee living in Indiana, you must use "/ in /" instead of "in".

in conclusion

Although AWK completely represents additional meaning, it should be the initial abbreviation of the administrator smart toolkit. With SED, the AWK utility is one of the most powerful and flexible tools owned by Linux administrators. By understanding some of their language, you can open up a field that simplifies tasks, otherwise these tasks will be very time and difficult.

转载请注明原文地址:https://www.9cbs.com/read-32337.html

New Post(0)