Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use sed and awk in regular expressions

Shulou Source: shulou.com Published: 2022-06-01 00:05:21 12月03日 Update

Editor to share with you how to use sed and awk in regular expressions, I believe most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it together.

II. Sed

The main way to use sed is to use its find and exchange functions.

Command format sed-n'/ /'p filename in single quotation marks

Create a directory and copy the passwd file in

Show only the lines we need to find-n

Specify an interval print

Implement multiple,-e

Find a file and list it regardless of case-I

Delete some lines and put the rest of the rows out d

Sed deletes the selected line-I

Globally specify to replace 1pm 10 lines s

S stands for substitution, plus g represents the global, and not adding g will only replace the first one that appears in this line.

Adjust string position-r

Adjust the position of the first and last paragraph

Add a character to the front of the document, such as aaa

III. Awk

The-F option specifies the delimiter, if no delimiter is added, with a space or tab as the delimiter

Print is a print action, $1 is the first paragraph $2 is the second paragraph $3 the third paragraph $0 represents all

Print everything

Match a character or string

Print the first line containing oo

Print multiple times or match multiple times

Conditional operator

, > =, < if you enclose a number in double parentheses when comparing with a number, it will not be considered a number

It will be thought of as a character, or a number without double parentheses.

Exact matching

List the lines whose third paragraph is greater than 500 and print

! = is not equal to, the seventh paragraph is not equal to / sbin/nologin.

You can use & & and |, denote and and, respectively, or

Awk built-in variables

OFS, similar to the-F option, also defines the split symbol, but it is defined at the time of output

NR represents the line number

NF indicates how many segments there are after being separated by a delimiter.

OFS usage:

NF usage: indicate how many segments, list the segment number

NR usage: indicate how many lines, list the line number

Awk mathematical operation

There is no cut delimiter, you can define the delimiter with OFS

Precise matching

Awk calculates the sum of a segment

These are all the contents of the article "how to use sed and awk in regular expressions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Separator character number content article regular expression representative position global string parenthesis file line number adjustment precision precision one line not much function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi Shulou Information Docker Shulou Technology