Table of contents
3. Sed stream editor (generally used and replaced text)
1、sed -i 's/#rm -f \/a\/b/rm -f \/a\/b/g' hello/file
2、 sed -i 's/^bind-address.*/bind-address = 0.0.0.0/' $MYSQL_CONF_FILE
3、 Sed -r 's/root/ROOT/gi/' /etc/passwd
Fourth, the use of awk (for output columns)
1. Regular expressions
. |
ls .txt |
Match files ending with .txt in the current directory |
* |
grep 'a*' file.txt |
Matches all consecutive occurrences of the letter a in the file |
+ |
grep 'ba+' file.txt |
All strings in the configuration file that contain a b followed by one or more a's. |
? |
grep 'colou?r' file.txt |
Matches strings containing color or colour in the file. |
[ ] |
grep ' [ credit ] ' file.txt |
Match any string containing any vowel in the file |
[^] |
’grep '[^0-9]' file.txt |
matches lines in the file that do not contain numbers |
- |
grep '[a-z]' file.txt |
Match strings containing lowercase letters in the file |
\b |
grep '\bword\b' file.txt |
Match individual words word in the file |
^ |
grep '^start' file.txt |
Matches lines starting with start in the file |
$ |
grep 'end$' file.txt |
Matches lines in a file that end with end |
Second, the use of grep
cat test.txt |grep ^u |
Find out the content of the line starting with u in test.txt |
cat test.txt |grep ^[^u] |
Find out the content of the line that does not start with u in test.txt |
cat /etc/passwd | grep -n root |
Take /etc/passwd, take out the line where root appears, and (-n) display the line number |
ps to | wc-l |
show the number of processes |
grep "wl" 1.txt 2.txt 3.txt |
Find wl in 1.txt, 2.txt, 3.txt |
grep -r "wl" path/inc |
Recursively search for wl in path/inc |
grep "wl" path/*.txt |
All txt in the path directory, search for wl |
3. The sed stream editor (generally used to replace text)
1、sed -i 's/#rm -f \/a\/b/rm -f \/a\/b/g' hello/file
-i: edit directly on the original file
s indicates the start of the replacement operation
g means global replacement (if there is no g, if multiple lines appear and there are multiple occurrences in one line, only the first one of each line will be replaced)
#rm -f \/a\/b/The pattern to be replaced is the text of #rm -f /a/b
rm -f \/a\/b/ is the replaced pattern, which is the text of rm -f /a/b
Indicates to replace #rm -f /a/b in hello/file with rm -f /a/b
2、 sed -i 's/^bind-address.*/bind-address = 0.0.0.0/' $MYSQL_CONF_FILE
^bind-address.*: Match lines beginning with "bind-address".
bind-address = 0.0.0.0: Replace the matching behavior "bind-address = 0.0.0.0".
3、 Sed -r 's/root/ROOT/gi/' /etc/passwd
For all files (-r) in the etc/passwd directory, replace all (g) root (s) with ROOT in text (i)
4. Escape characters
See -r '\crootcd' passwd
Sed -r '\#root#d' passwd (\ as an escape character, telling sed to treat # as an ordinary character)
Delete the root in passwd, and the two # can be replaced with any character
Fourth, the use of awk (for output columns)
awk '{print $2}' file.txt |
print the second column in file.txt |
awk -F':' '{print $1, $NF}' /etc/passwd |
With : as separator, print the first and last column |
awk -va=1 '{print $1,$1+a}' log.txt |
-va=1, specify the variable a, assign a value of 1, then output the contents of the first column and the second column |