当前位置:网站首页>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~~~
边栏推荐
- Maxwell equation and Euler formula - link
- Druids connect to mysql8.0.11
- 1 Introduction to spark Foundation
- IO flow review
- A treasure open source software, cross platform terminal artifact tabby
- Xiangong intelligent obtained hundreds of millions of yuan of b-round financing to accelerate the process of building non-standard solutions with standardized products
- C # basic knowledge (1)
- [Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)
- "Learning notes" recursive & recursive
- Classification and extension of OC
猜你喜欢
How to switch between dual graphics cards of notebook computer
How to solve win10 black screen with only mouse arrow
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Buuctf, misc: n solutions
The difference between single power amplifier and dual power amplifier
The reason why the computer runs slowly and how to solve it
Format cluster and start cluster
2022.02.14
Recursive least square adjustment
2022.02.13
随机推荐
[template summary] - binary search tree BST - Basics
A preliminary study on the middleware of script Downloader
C deep anatomy - the concept of keywords and variables # dry inventory #
Learning methods of zynq
NPM script
[15th issue] Tencent PCG background development internship I, II and III (OC)
Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
How to prevent malicious crawling of information by one-to-one live broadcast source server
How to switch between dual graphics cards of notebook computer
[note] glide process and source code analysis
Opengauss database log management guide
Blue Bridge Cup -- guess age
Schematic diagram of crystal oscillator clock and PCB Design Guide
Codeforces Round #768 (Div. 1)(A-C)
. Net ADO splicing SQL statement with parameters
MLX90614 driver, function introduction and PEC verification
How to quickly build high availability of service discovery
C # basic knowledge (2)
Classification and extension of OC
[untitled]