Linux study notes---day8 (3.27) sed, awk and regular expressions

There are two forms of calling sed

sed [-nefri] 'command' file(s)
sed [-nefri] -f scriptfile file(s)

-n : Quiet mode. In normal sed syntax, all data from STDIN will be listed on the screen. But after adding the -n parameter, only the line that has been specially processed by sed will be listed.

-e : Edit the action of sed directly in the command line mode

-f : write the sed action directly in a file, -f filename can execute the sed action in filename

-r : sed action support is the syntax of extended regular notation

-i : directly modify the content of the read file instead of outputting it on the screen


Common commands

a : new, a can be followed by a string, and these strings will appear on a new line (the current next line)

c : replace, c can be followed by strings, these strings replace the lines between n1 and n2

d : delete, usually nothing after d

i : Insert, i can be followed by strings, and these strings will appear on a new line (the current previous line)

p : print, print out a selection, usually p works with -n

s : Replace, you can directly perform the replacement work, usually the action of this s can be matched with regular notation

Search for data and execute commands

Search /etc/passwd, find the line corresponding to root, and execute a set of commands in the curly brackets. Each command is separated by a semicolon. Here, replace bash with blueshell, and then output this line:

nl  /etc/passwd  | sed -n '/root/{s/bash/blueshell/;p}'


awk

Compared with sed, which is often processed as a whole line, awk prefers to divide a line into several fields to process. Therefore, awk is quite suitable for small data processing.

awk [options] 'script' var=value file(s)
awk [options] 'scriptfile' var=value file(s)


-F fs fs specifies the input separator, fs can be a string or a regular expression

-v var=value Assign a user-defined variable, pass the external variable to awk

-f scriptfile read awk commands from script file



awk scripts are made up of patterns and actions

BEGIN : Let the user specify the action to take place before the first input record is processed, usually a global variable can be set here

END : Let the user take action after the last input record has been read


operate
An action consists of one or more commands, functions, expressions, separated by newlines or semicolons, and enclosed in curly braces. There are four main parts:
• Variable or array assignment
• output commands
• Built-in functions
• Control flow commands


awk script basic structure

awk  'BEGIN{ print "start" } pattern{ commands } END{ print "end" }' file

An awk script usually consists of three parts: BEGIN statement block, general statement block that can use pattern matching, and END statement block. These three parts are optional.

Execution principle:
Step 1: Execute the statements in the BEGIN{ commands } statement block;
Step 2: Read a line from the file or standard input (stdin), then execute the pattern{ commands } block, which scans the file line by line, repeating the process from the first line to the last line, until the file has been read in its entirety.
Step 3: When reading to the end of the input stream, execute the END{ commands } statement


awk built-in variables

$n : The nth field of the current record. For example, when n is 1, it means the first field

$0 : This variable contains the text content of the current line during execution

FNR : same as NR, but relative to the current file

FS : Field separator (default is any space)

NF: Indicates the number of fields, which corresponds to the current number of fields during execution

NR: Indicates the number of records, which corresponds to the current line number during execution

OFS : output field separator (default is a space)

RS : record separator (default is a newline)

ORS : output record separator (default is a newline)










Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813411&siteId=291194637