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

Regular expression

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

Grep

It's mainly used for filtering.

Cp / etc/passwd 1.txt

Grep-- color 'root' 1.txt # prints matching lines and displays them in color

Vim .bashrc

# add

Alias grep='grep-color'

Grep-c 'root' 1.txt # filter line number

Grep-n 'root' 1.txt # prints the line that meets the requirements and displays the line number

Grep-v 'root' 1.txt # upside down, printing does not match the line you want to go

Grep-E = egrep # indicates a special symbol such as () +?, do not escape, escape symbol\

-A: followed by a number (with or without spaces). For example,-A2 prints lines that meet the requirements and the following two lines

-B: followed by a number. For example,-B2 means to print the lines that meet the requirements and the above two lines.

-C: followed by a number. For example,-C2 means to print lines that meet the requirements and two lines up and down.

-r: traverses all the files under the directory

Grep 'r.t' 1.txt # r and t must be printed with a character in the middle

Grep 'rystt' 1.txt # means t must print, r can have zero or more

Grep 'r.roomt' 1.txt # r and t must be printed, with any character in the middle

Grep'[0-9] '1.txt # prints all lines containing numbers

Grep'[^ 0-9] '1.txt # prints all non-numeric lines, such as characters, blank lines

Grep'^ [0-9] '1.txt # prints all lines that start with a number

Grep'[0-2] 'txt = grep' [0min1d2] '1.txt

Grep'^ $'1.txt # print blank lines

Grep 'in$' 1.txt # prints lines ending in in

Grep'[a Murz] '1.txt # prints all small letter lines

Grep'[a-zA-Z] '1.txt # prints lines of all uppercase and small letters

Grep 'root\ | mysql' 1.txt # or, but to escape\

^: what does it start with

$: how does it end

*: means * the character preceding it can have zero or more characters.

. *: any character

+: one or more characters preceding +

Zero or one? The preceding character

Characters that need to be escaped:? + () []\

Sed

Grep can be used by sed, but sed has more functions. You can delete d and replace g.

-r means to use regular special symbols without ambiguity.

Sed '10roomp-n 1.txt # p is printed,-n cancels automatic printing, prints lines as required, and repeats printing without-n

P is usually used with-n, that is, printing is usually used with-n.

Sed-n'/ root/p' 1.txt # prints a line containing a character. You can use regular ^, * $, and / / when you want to use the key word.

-e can be used to perform multiple tasks at the same time, sed-eBay / body/p'-n 1.txt, also can be used.

Sed-n'/ root/p;/body/p' 1.txt

Sed'/ root/d' 1.txt # Delete lines

Sed '1d' 1.txt # delete the first line and print the rest

Sed '1Jing 10d' 1.txt # delete the first line to the tenth line and print the rest

Sed'1 sed 2sCharger to 1.txt # s replace, g global replace, otherwise only replace the first line in 1-2 lines, replace ot with to, the rest can be printed as usual, / can be #, @, these only take effect if the replacement takes effect

Sed's / [0-9] / / g '1.txt # delete all numbers, that is, replace them with empty ones

Sed'/ [0-9] / d'1.txt # deletes all numeric lines

Sed's / [^ 0-9] / / g '1.txt # delete all non-numbers, that is, replace the number with an empty one

Head-N2 1.txt | sed's/\ (root\)\ (. *\)\ (bash\) /\ 3\ 2\ 1 swap the positions of two strings, note the escape symbols, and there are three /

Sed's / ^. * $/ & loging/g' 1.txt # add loging at the end, it must be replaced before it can be added

Sed-I's 1.txt to Universe directly modify the contents of the file

Sed exercises:

Copy / etc/passwd to / root/test.txt

Print all lines with sed: sed'/. * / p'- n 1.txt

Print 3 to 10 lines of test.txt: sed '3Jing 10p'-n 1.txt

Print the line containing 'root'' in test.txt: sed / root/p'-n 1.txt

Delete 15 lines of test.txt and all subsequent lines: sed'15 1.txt

Delete the line in test.txt that contains' bash'; sed'/ bash/d' 1.txt

Replace 'root' with' toor' in test.txt

Replace'/ sbin/nologin' to'/ bin/login' in test.txt

Delete all the numbers from lines 5 to 10 in test.txt: sed-e's / [0-9] / / g'- e '5je 10p'-n 1.txt

Delete all special characters (except numbers and uppercase and lowercase letters) from test.txt: sed's / [^ a-zA-Z0-9] / / g '1.txt

Change the position of the first word and the last word in test.txt: sed-r's / (^. *) (:. *:) (. * $) /\ 3\ 2\ 1 1.txt

Replace the first number and the last word in test.txt

Move the first number in test.txt to the end of the line

Add 'aaa:':sed-e's / ^. * $/ & aaa/g'-e' 10p'- n 1.txt to the end of line 20 of test.txt

Awk

You can print by segment

Awk-F':'{print $1} '1.txt # intercepts the first paragraph of the document with: as the delimiter

Awk-F':'{print $1 "#" $2} '1.txt # uses: as the delimiter, intercepts the first and second paragraphs of the document and delimits them with #. Custom delimiters can only be defined in double quotation marks, not single quotation marks.

Awk-F': 'OFS= "@" {print$1,$3}' 1.txt # and the above meaning

Awk'/ oo/' 1.txt # matches a character or string

Awk-F':'$1 ~ / oo/' 1.txt # match for a certain segment

Awk'/ root | mysql/' 1.txt # matches multiple

Awk-F':'/ root/ {print $1 camera 3}; $1 MySQL 1.txt # match multiple times and show each match.

Awk-F':'$1~/r*o/ {print $3} '1.txt # prints matching lines

The conditional operator = =, >, =, which should be in the form 'root@/bin/bash'): awk-F':''OFS= "@" {print $1 minus 7}' 1.txt

Use':'as the delimiter to add the fourth paragraph of the entire document to sum:

Awk-F': 'sum=sum+$4 {print sum}' 1.txt | tail-N1

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

Network Security

Wechat

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

12
Report