当前位置:网站首页>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~~~
边栏推荐
- Why should enterprises do more application activities?
- C # basic knowledge (2)
- 540. Single element in ordered array
- 3 environment construction -standalone
- pycuda._ driver. LogicError: explicit_ context_ dependent failed: invalid device context - no currently
- Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
- How to quickly build high availability of service discovery
- Shell script three swordsman awk
- [15th issue] Tencent PCG background development internship I, II and III (OC)
- ThreadLocal function, scene and principle
猜你喜欢

Summary of fluent systemchrome

Hcip day 12 notes

string

Scratch uses runner Py run or debug crawler

Learning notes of raspberry pie 4B - IO communication (SPI)

Runtime. getRuntime(). totalMemory/maxMemory()

1 Introduction to spark Foundation
![[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)](/img/b8/e2a59772d009b6ee262fb4807f2cd2.jpg)
[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)

320. Energy Necklace (ring, interval DP)

How to restore the factory settings of HP computer
随机推荐
Why should enterprises do more application activities?
User login function: simple but difficult
Harbor integrated LDAP authentication
Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet
Firefox set up proxy server
在恒泰证券开户怎么样?安全吗?
How to prevent malicious crawling of information by one-to-one live broadcast source server
ADB related commands
320. Energy Necklace (ring, interval DP)
Leetcode: a single element in an ordered array
Go Technology Daily (2022-02-13) - Summary of experience in database storage selection
Maxwell equation and Euler formula - link
Flutter internationalized Intl
Hcip 13th day notes
Recursive least square adjustment
How to quickly build high availability of service discovery
How about opening an account at Hengtai securities? Is it safe?
Qtoolbutton available signal
Ppt image processing
Blue Bridge Cup -- Mason prime