Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use sed and awk in regular expressions

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report