当前位置:网站首页>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~~~
边栏推荐
- How can enterprises and developers take advantage of the explosion of cloud native landing?
- Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~
- NPM script
- Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
- SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
- Current detection circuit - including op amp current scheme
- Teach you how to run two or more MySQL databases at the same time in one system
- Interesting 10 CMD commands
- string
- Buuctf, misc: n solutions
猜你喜欢
Loop compensation - explanation and calculation of first-order, second-order and op amp compensation
Buuctf, misc: sniffed traffic
webAssembly
320. Energy Necklace (ring, interval DP)
How to understand the gain bandwidth product operational amplifier gain
Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet
Fluent learning (5) GridView
How to quickly build high availability of service discovery
Design of logic level conversion in high speed circuit
User login function: simple but difficult
随机推荐
Live app source code, jump to links outside the station or jump to pages inside the platform
Firefox set up proxy server
finalize finalization finally final
How about agricultural futures?
Bufferpool caching mechanism for executing SQL in MySQL
Teach you how to run two or more MySQL databases at the same time in one system
Yyds dry goods inventory Prometheus alarm Art
2022.02.14
Niuke winter vacation training camp 4 g (enumeration optimization, Euler power reduction)
IO flow review
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"
Druids connect to mysql8.0.11
How to quickly build high availability of service discovery
Format cluster and start cluster
How can enterprises and developers take advantage of the explosion of cloud native landing?
Shiftvit uses the precision of swing transformer to outperform the speed of RESNET, and discusses that the success of Vit does not lie in attention!
2.14 summary
Flutter internationalized Intl
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant