In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
It is believed that many inexperienced people have no idea about how to use sed text processing commands in Linux. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Sed's handling of text is very powerful, and the sed is very small, few parameters, easy to grasp, his way of operation is a bit like root awk. Sed reads files sequentially, line by line. It then performs all the operations specified for the row, and the contents are displayed after the requested modification is completed, or it can be stored in a file. After completing all the operations on one line, it reads the next line of the file and then repeats the process until it finishes the file. It is important to note here that the source file (by default) remains unmodified. Sed reads the entire file by default and modifies each line in it. To put it bluntly, it is a line-by-line operation.
Parameters.
Sed-h
-n,-- quiet,-- silent cancel automatic printing mode space
-e script,-- expression= script adds "script" to the running list of the program
-f script file,-- file= script file adds "script file" to the running list of the program
-- follow-symlinks follows soft links when modifying files directly
-I [extension],-- in-place [= extension] modify the file directly (back up the file if you specify the extension)
-l N,-- line-length=N specifies the expected length of line breaks for the "l" command
-- posix closes all GNU extensions
-r,-- regexp-extended uses extended regular expressions in scripts
-s,-- separate treats the input file as a separate file rather than a long continuous input
-u,-- unbuffered reads the least data from the input file and refreshes the output more frequently
-- help print help and exit
-- version outputs version information and exits
Example 1
Test file
The code is as follows:
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/bin/false
Daemon:x:2:2:daemon:/sbin:/bin/false
Mail:x:8:12:mail:/var/spool/mail:/bin/false
Ftp:x:14:11:ftp:/home/ftp:/bin/false
& nobody:$:99:99:nobody:/:/bin/false
Zhangy:x:1000:100:,:/home/zhangy:/bin/bash
Http:x:33:33::/srv/http:/bin/false
Dbus:x:81:81:System message bus:/:/bin/false
Hal:x:82:82:HAL daemon:/:/bin/false
Mysql:x:89:89::/var/lib/mysql:/bin/false
Aaa:x:1001:1001::/home/aaa:/bin/bash
Ba:x:1002:1002::/home/zhangy:/bin/bash
Test:x:1003:1003::/home/test:/bin/bash
@ zhangying:*:1004:1004::/home/test:/bin/bash
Policykit:x:102:1005:Po
Example a, this example, replaces root in the test file with tankzhang, but only once and terminates the operation on this line, go to the next line
The code is as follows:
[zhangy@BlackGhost mytest] # sed's Universe rootUniverse tankzhang test | grep tank
Tankzhang:x:0:0:root:/root:/bin/bash
Example b, in this example, replace all the root in the file test with tankzhang. Please note the letter g, the abbreviation of global.
The code is as follows:
[zhangy@BlackGhost mytest] # sed's test rootbank Tankzhang test | grep zhang
Tankzhang:x:0:0:tankzhang:/tankzhang:/bin/bash
Zhangy:x:1000:100:,:/home/zhangy:/bin/bash
Ba:x:1002:1002::/home/zhangy:/bin/bash
@ zhangying:*:1004:1004::/home/test:/bin/bash
Example c, adding-n p means that only those lines that are replaced (partial substitution) are printed. In the above example, I did not add grep.
The code is as follows:
[zhangy@BlackGhost mytest] # sed-n's test
Tankzhang:x:0:0:root:/root:/bin/bash
Example d, adding-n pg means that only those lines that are replaced (all replace) are printed. In the above example, I did not add grep.
The code is as follows:
[zhangy@BlackGhost mytest] # sed-n's test
Tankzhang:x:0:0:tankzhang:/tankzhang:/bin/bash
Example e, between the second and eighth lines, replace the line that begins with zhang, replace it with ying, and display the replaced line
The code is as follows:
[zhangy@BlackGhost mytest] # cat test | sed-ne '2Jing 8s / ^ Zhang / ying/gp'
Yingy:x:1000:100:,:/home/zhangy:/bin/bash
Example f, when there are multiple commands to be executed, can be separated by a semicolon, and the delimiter can be customized, the default is /. The above example means that between the second and eighth lines, replace the line that begins with zhang, replace it with ying, between 5 and 10, replace dbus with goodbay, and display the replacement line.
The code is as follows:
[zhangy@BlackGhost mytest] # cat test | sed-n '2Jing 8s / ^ Zhang / ying/gp;5,10s#dbus#goodbay#gp'
Yingy:x:1000:100:,:/home/zhangy:/bin/bash
Goodbay:x:81:81:System message bus:/:/bin/false
Example g, the root of this example is the same as the example above, except that-e acts as a semicolon, and-e can also split multiple commands.
The code is as follows:
[zhangy@BlackGhost mytest] # cat test | sed-ne '2ZHANGCHANGCHANGCHAGING GPP'-ne'5 10s recording dbushes goodbayhands gp'
Yingy:x:1000:100:,:/home/yingy:/bin/bash
Goodbay:x:81:81:System message bus:/:/bin/false
Example h, regular usage, add\ if you use parentheses in sed, otherwise you will report an error.
The code is as follows:
[zhangy@BlackGhost mytest] # sed-ne '2jiggle 8s / ^\ (zhangy\) /\ 1ingAccord gp' test
Zhangying:x:1000:100:,:/home/zhangy:/bin/bash
[root@masters ~] # sed-ne'2 recorder 8s / ^\ (zhangy\) / & ing/gp' test
Zhangying:x:1000:100:,:/home/zhangy:/bin/bash
The use of izhang & is to add the string after & to the string found, and ying after the string.
The code is as follows:
[zhangy@BlackGhost mytest] # sed-ne'2 relegation 15s ZHANGCHANGCHANGYINGG 'test
Zhangyingy:x:1000:100:,:/home/zhangyingy:/bin/bash
Ba:x:1002:1002::/home/zhangyingy:/bin/bash
@ zhangyingying:*:1004:1004::/home/test:/bin/bash
Example j, this example is to replace between lines starting with zhang and ending with lines that match Po
The code is as follows:
[zhangy@BlackGhost mytest] # sed-ne'/ ^ zhang/,/Po/s/zhang/ying/gp' test
Yingy:x:1000:100:,:/home/yingy:/bin/bash
Ba:x:1002:1002::/home/yingy:/bin/bash
@ yingying:*:1004:1004::/home/test:/bin/bash
Here n is the abbreviation of next. After finding the line of root, replace the bin in the next line with tank.
The code is as follows:
[zhangy@BlackGhost mytest] $sed'/ root/ {nbett _ Tank /} 'test
Root:x:0:0:root:/root:/bin/bash
Tank:x:1:1:bin:/bin:/bin/false
The function of the example mpenery y is to change the matching character to uppercase, but the length of the replacement character is the same as that of the character to be replaced.
The code is as follows:
[zhangy@BlackGhost mytest] $sed-e'1 test 2y test
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
Bin:x:1:1:bin:/bin:/bin/false
For example, the function of nQuery h is to put the found lines into a cache, and the function of G is to put the contents of the cache on the last line.
The code is as follows:
[zhangy@BlackGhost mytest] $sed-e'/ root/h'-e'$G' test
..
..
Ba:x:1002:1002::/home/zhangy:/bin/bash
Test:x:1003:1003::/home/test:/bin/bash
@ zhangying:*:1004:1004::/home/test:/bin/bash
Root:x:0:0:root:/root:/bin/bash
Example o, line substitution, replace the line matching root with the line matching zhangy
The code is as follows:
[zhangy@BlackGhost mytest] $sed-e'/ root/h'-e'/ zhangy/g' test
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/bin/false
Daemon:x:2:2:daemon:/sbin:/bin/false
Mail:x:8:12:mail:/var/spool/mail:/bin/false
Ftp:x:14:11:ftp:/home/ftp:/bin/false
& nobody:$:99:99:nobody:/:/bin/false
Root:x:0:0:root:/root:/bin/bash
Http:x:33:33::/srv/http:/bin/false
Dbus:x:81:81:System message bus:/:/bin/false
Hal:x:82:82:HAL daemon:/:/bin/false
Mysql:x:89:89::/var/lib/mysql:/bin/false
Aaa:x:1001:1001::/home/aaa:/bin/bash
Root:x:0:0:root:/root:/bin/bash
Test:x:1003:1003::/home/test:/bin/bash
Root:x:0:0:root:/root:/bin/bash
Example p, this example is to replace between lines starting with zhang and ending with lines that match Po
The code is as follows:
[zhangy@BlackGhost mytest] # sed-ne'/ ^ zhang/,/Po/s/zhang/ying/gp' test
Yingy:x:1000:100:,:/home/yingy:/bin/bash
Ba:x:1002:1002::/home/yingy:/bin/bash
@ yingying:*:1004:1004::/home/test:/bin/bash
The example qforce 3Q means to exit on the third line.
The code is as follows:
[zhangy@BlackGhost mytest] $sed-e's Bintank test
Root:x:0:0:root:/root:/tank/bash
Tank:x:1:1:tank:/tank:/tank/false
Daemon:x:2:2:daemon:/stank:/tank/false
Example r, special matching
Don't forget that there is a square bracket outside the square brackets to match the numbers.
[: alnum:] alphanumeric [amurz Amurz 0-9]
[: alpha:] letter [amurz Amurz]
[: blank:] spaces or tabs
[: cntrl:] any control character
[: digit:] numbers [0-9]
[: graph:] any visual character (no spaces)
[: lower:] lowercase [amurz]
[: print:] non-control character
[: punct:] punctuation character
[: space:] Spac
[: upper:] uppercase [Amurz]
[: xdigit:] hexadecimal digits [0-9 amurf Amurf]
The code is as follows:
[zhangy@BlackGhost mytest] # sed-ne'2 sed 15s Dropzhangy.* [[: digit:]] / = / gp' test
=:,: / home/zhangy:/bin/bash
@ =:: / home/test:/bin/bash
Example 2
For example a, delete line 1Time14
The code is as follows:
[zhangy@BlackGhost test] $sed-e'1 test 14d'
@ zhangying:*:1004:1004::/home/test:/bin/bash
Policykit:x:102:1005:Po
For example b, delete the lines after 4, including line 4, and take $as the maximum number of lines.
The code is as follows:
[zhangy@BlackGhost mytest] $sed-e '4jade d' test
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/bin/false
Daemon:x:2:2:daemon:/sbin:/bin/false
For example c, delete the line that includes false or the line that includes bash, and don't forget to add\
The code is as follows:
[zhangy@BlackGhost mytest] $sed-e'/\ (false\ | bash\) $/ d'test
Policykit:x:102:1005:Po
Example d, delete the line from the line that matches root to the line that matches the middle line that starts with test
The code is as follows:
[zhangy@BlackGhost mytest] $sed-e'/ root/,/ ^ test/d' test
@ zhangying:*:1004:1004::/home/test:/bin/bash
Policykit:x:102:1005:Po
Example 3
Example a, read the contents of test2 and write it to the bottom of the matching line
The code is as follows:
[zhangy@BlackGhost mytest] $sed-e'/ ^ root/r test2' test
Root:x:0:0:root:/root:/bin/bash
=
-
+
Bin:x:1:1:bin:/bin:/bin/false
Daemon:x:2:2:daemon:/sbin:/bin/false
Example b, write rows that match numbers into test2
The code is as follows:
[zhangy@BlackGhost mytest] $sed'/ [[: digit:]] / w test2' test
Example c, what you are going to insert, insert below the matching line
The code is as follows:
[zhangy@BlackGhost mytest] $sed'/ root/a\\ = = aaaa====' test
Root:x:0:0:root:/root:/bin/bash
= aaaa====
Bin:x:1:1:bin:/bin:/bin/false
Example d, exactly the opposite of root a, what is about to be inserted is inserted on top of the matching line.
The code is as follows:
[zhangy@BlackGhost mytest] $sed'/ ^ daemon/i\\ = 'test
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/bin/false
=
Daemon:x:2:2:daemon:/sbin:/bin/false
Mail:x:8:12:mail:/var/spool/mail:/bin/false
Example 4
# get the parent directory of a file (or directory) path, swatches @ is the replacement format,\ (/. * /\) refers to a "/" followed by any character followed by a "/", where\ (\) is used to reference the matching content as a whole. [^ /]\ {1,\} means that a non-"/" character appears once, twice, or more times; /\? It means that "/" occurs 0 or 1 times, and\ 1 refers to the matching content in front of the backward reference.
The code is as follows:
[root@practice ~] # echo "/ usr/local/bin/" | sed's @\ (/. * /\) [^ /]\ {1,\} /\? @\ 1percent'
/ usr/local/
# after using extended regular expressions, you can do the same:
[root@practice ~] # echo "/ etc/rc.d/rc.sysinit" | sed-r's @ (/. * /) [^ /] + /? @\ 1regions'
/ etc/rc.d/
After reading the above, have you mastered how to use sed text processing commands in Linux? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.