Linux how to use the text processing tool awk

  

awk is a text processing tool in Linux, similar to the shell programming language, has its own language, the following small series will introduce you to the use of awk text processing tools in Linux, interested Friends can come and find out.

Called

1. command

awk [-F field-separator] & lsquo; commands & rsquo; input-file (s)

commands are true awk commands, [-F field separators] are optional. Input-file(s) is the file to be processed. In awk, in each line of a file, each item separated by a domain separator is called a field. Typically, in the case of the unnamed -F domain separator, the default domain separator is a space (or tab).

2.shell script mode

Insert all awk commands into a file and make the awk program executable, then the awk command interpreter as the first line of the script, by typing the script name transfer. Equivalent to the first line of the shell script: #! /bin/sh can be replaced with: #! /bin/awk -f

3. Insert all awk commands into a single file, then call: awk -f awk-script-file input-file(s) where -f option loads awk-script The awk script in -file, input-file(s) is the same as above.

Built-in variables

ARGC: Number of command line arguments

ARGV: Command line arguments

ENVIRON: UNIX environment variables

ERRNO: UNIX system error message

FILENAME: filename of awk browse

OFMT: Number output format %.6g

FNR: Number of records for browsing files

FS: Set the input field separator, equivalent to the command line -F option

NF: The number of fields to view the record

NR: The number of records read, that is, the line Number, starting from 1

FNR: Current number of records

OFS: Output field separator

ORS: Output record separator

RS: Control record Separator

$0: Current record (as a single variable)

$1~$n: The nth field of the current record, separated by FS

Output function< Br>

print: Parameters can be variables, values ​​or strings. Strings must be quoted in double quotes, separated by commas. Without a comma, the parameters are concatenated and cannot be distinguished. Here, the role of the comma is the same as the separator of the output file, but the latter is a space.

printf: its usage is basically similar to printf in c language, you can format the string, the output is complex , printf is easier to use, the code is easier to understand. The specific format is as follows:

%d decimal signed integer

%u decimal unsigned integer

%f floating point number

%s string

%c single character

%p pointer value

%e exponential floating point number

%x %X unsigned in hexadecimal Integer

%o Unsigned integer in octal

%g Automatically select the appropriate representation

Get external variable

1. Get normal External variables

awk ‘{action}’ variable name=variable value

2.BEGIN block variable

awk –v variable name=variable value[ –v variable 2=value 2 …] ‘BEGIN{action}’

3.Environment variables

Need to use built-in variable ENVIRON implementation
Previous123456 One page total of 6 pages

Copyright © Windows knowledge All Rights Reserved