当前位置:网站首页>2/14 (regular expression, sed streaming editor)
2/14 (regular expression, sed streaming editor)
2022-07-03 23:10:00 【paul__ george】
Regular expressions
1.
head -5 /etc/passwd > user # Prepare the material
grep “[root]” user # look for r、o、t Any character
grep “[rot]” user # The effect same as above
grep “[^rot]” user # Show r or o or t Beyond
grep “[0123456789]” user # Find all the numbers
grep “[0-9]” user # The effect same as above
grep “[^0-9]” user # Show content other than numbers
grep “[a-z]” user # Find all lowercase letters
grep “[A-Z]” user # Find all capital letters
grep “[a-Z]” user # Find all the letters
grep “[^0-9a-Z]” user # Find all the symbols
grep “.” user # Find any single character , Every character in the document can be understood as any character
grep “r…t” user # look for rt There are 2 A line of arbitrary characters
grep “r.t” user # look for rt There are 1 A line of arbitrary characters , There is no match , There is no output
grep “" user # Incorrect usage , The number matches the previous character any time , Not to be used alone
grep "rot" user # look for rt, In the middle of the o It's ok if there's any , A few times will do
grep ".” user # Find any , Including the empty line . And * The combination of is equivalent to the effect of wildcards in regularization
grep “ro{1,2}t” user # look for rt, In the middle of the o There can be 1~2 individual
grep “ro{2,6}t” user # look for rt, In the middle of the o There can be 2~6 individual
grep “ro{1,}t” user # look for rt, In the middle of the o There can be 1 And 1 More than
grep “ro{3}t” user # look for rt, In the middle of the o There must be only 3 individual
2.
All the above commands can add -E Option and remove all \, Change to the extended regular usage , such as
grep “ro{1,}t” user You can change to grep -E “ro{1,}t” user
perhaps egrep “ro{1,}t” user
grep “ro{1,}t” user # Using basic regular search o appear 1 Times and 1 More than once
egrep “ro{1,}t” user # Using extended regularization , The effect same as above , It is relatively concise
egrep “ro+t” user # Using extended regularization , The effect same as above , Minimalist
grep “roo{0,1}t” user # Use basic regularity to find the second o appear 0~1 Time
egrep “roo{0,1}t” user # Using extended regularization , The effect same as above , It is relatively concise
egrep “roo?t” user # Using extended regularization , The effect same as above , Minimalist
egrep “({2}” user # Find continuous 2 individual 0: The function of parentheses is to combine characters into a whole
egrep “root|bin” user // Look for root perhaps bin The line of
egrep “the\b” abc.txt // stay abc.txt Find... In the file the, The right side is not allowed to go out
Now the number 、 Letter 、 Underline
egrep “\bthe\b” abc.txt // Numbers are not allowed on either side 、 Letter 、 Underline
egrep “<the>” abc.txt // The effect same as above
sed Streaming editor
1, Pre instruction | sed Options Conditions Instructions
2,sed Options Conditions Instructions Processed documents
Options -n Mask default output -r Support extended regularization -i Modify source file
Instructions p Output d Delete s Replace
Conditions Line number / character string /
1) p Output
sed -n ‘p’ user # Output all lines
sed -n ‘1p’ user # Output No 1 That's ok
sed -n ‘2p’ user # Output No 2 That's ok
sed -n ‘3p’ user # Output No 3 That's ok
sed -n ‘2,4p’ user # Output 2~4 That's ok
sed -n ‘2p;4p’ user # Output No 2 Lines and 4 That's ok
sed -n ‘3,+1p’ user # Output No 3 Lines and after 1 That's ok
sed -n ‘/^root/p’ user # Output to root Beginning line
sed -n ‘/root/p’ user # The output contains root The line of
sed -nr ‘/root|bin/p’ user # Output to root The first line or bin Beginning line ,| It's extended regular , need r Options
sed -n ‘1!p’ user # Output in addition to 1 The content of the line ,! It's a reversal
sed -n 'KaTeX parse error: Expected 'EOF', got '#' at position 11: p' user #̲ Output the last line sed -n '…= It's the line number of the last line
sed -n ‘$=’ user # Total rows of output file
2) d Delete ( If you remove -n, Will be p Change the command to d The instruction is to delete )
Delete the first 3~5 That's ok sed ‘3,5d’ user
Delete all include xml The line of sed ‘/xml/d’ user
Delete does not contain xml The line of sed ‘/xml/!d’ user
Delete with install Beginning line sed ‘/^install/d’ user
Delete the last line of the file sed ‘ d ′ u s e r Delete except The first 3 5 That's ok s e d ′ 3 , 5 d ′ a b c . t x t Delete except the Yes package contain x m l Of That's ok s e d ′ / x m l / d ′ a b c . t x t Delete except No package contain x m l Of That's ok s e d ′ / x m l / ! d ′ a b c . t x t Delete except With i n s t a l l open head Of That's ok s e d ′ / i n s t a l l / d ′ a b c . t x t Delete except writing Pieces of Of most after One That's ok s e d ′ d' user Delete the first 3~5 That's ok sed '3,5d' abc.txt Delete all include xml The line of sed '/xml/d' abc.txt Delete does not contain xml The line of sed '/xml/!d' abc.txt Delete with install Beginning line sed '/^install/d' abc.txt Delete the last line of the file sed ' d′user Delete except The first 3 5 That's ok sed′3,5d′abc.txt Delete except the Yes package contain xml Of That's ok sed′/xml/d′abc.txt Delete except No package contain xml Of That's ok sed′/xml/!d′abc.txt Delete except With install open head Of That's ok sed′/install/d′abc.txt Delete except writing Pieces of Of most after One That's ok sed′d’ user
Delete all blank lines sed ‘/^$/d’ user
3)s Replace
sed ‘s/2017/6666/’ shu.txt # Put the first of all lines 1 individual 2017 Replace with 6666
sed ‘s/2017/6666/2’ shu.txt # Put the first of all lines 2 individual 2017 Replace with 6666
sed ‘1s/2017/6666/’ shu.txt # The first 1 OK, No 1 individual 2017 Replace with 6666
sed ‘3s/2017/6666/3’ shu.txt # The first 3 OK, No 3 individual 2017 Replace with 6666
sed ‘s/2017/6666/g’ shu.txt # All... Lines 2017 Replace all
sed ‘/2024/s/2017/6666/g’ shu.txt # Find something that contains 2024 The line of , Put everything in it 2017 Replace with 6666
Example : If you want to put the /bin/bash Replace with /sbin/sh How to operate ?
sed -i ‘1s/bin/sbin/’ user // Traditional methods can be changed one by one , Change one first
sed -i ‘1s/bash/sh/’ user // Another one
sed ‘s!/bin/bash!/sbin/sh!’ user // Best solution , change s The replacement character of
sed ‘s(/bin/bash(/sbin/sh(’ user // The replacement symbol can use the... On all numeric keys
Write a script , build httpd service , use 82 Port number opens the service 
yum -y install httpd &> /dev/null // Install web site
echo “sed-test~~~” > /var/www/html/index.html // Define the default page
sed -i ‘/^Listen 80/s/0/2/’ /etc/httpd/conf/httpd.conf // Modify the configuration file , Change the listening port to 82
Then run the script
curl 192.168.2.5:82 // After the script runs , test 82 The port can see the page
sed-test~~~
边栏推荐
- SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
- 在恒泰证券开户怎么样?安全吗?
- IO flow review
- C3p0 connection MySQL 8.0.11 configuration problem
- X Opencv feature point detection and matching
- 2.14 summary
- Programming language (2)
- [note] IPC traditional interprocess communication and binder interprocess communication principle
- 1068. Consolidation of ring stones (ring, interval DP)
- Recursive least square adjustment
猜你喜欢

How to quickly build high availability of service discovery

The reason why the computer runs slowly and how to solve it

X Opencv feature point detection and matching

Firefox set up proxy server

Current detection circuit - including op amp current scheme

How to solve win10 black screen with only mouse arrow

2 spark environment setup local

Shell script three swordsman awk

Programming language (1)

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
随机推荐
[Android reverse] use DB browser to view and modify SQLite database (download DB browser installation package | install DB browser tool)
Creation of the template of the password management software keepassdx
Unsafe and CAS principle
Summary of fluent systemchrome
1068. Consolidation of ring stones (ring, interval DP)
X Opencv feature point detection and matching
Blue Bridge Cup -- guess age
Sow of PMP
QT creator source code learning note 05, how does the menu bar realize plug-in?
Current detection circuit - including op amp current scheme
How to understand the gain bandwidth product operational amplifier gain
Ten minutes will take you in-depth understanding of multithreading. Multithreading on lock optimization (I)
Arc135 partial solution
User login function: simple but difficult
Buuctf, misc: n solutions
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Yyds dry goods inventory Prometheus alarm Art
How to solve the problem of requiring a password when accessing your network neighborhood on your computer
Pyqt5 sensitive word detection tool production, operator's Gospel
[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)