effect
- sed command is a non interactive line text editor, which can edit the file content. By default, one line of content matched in the text file is processed to the mode space each time, and then the following commands are used to operate. After the operation is completed, the content in the mode space will be output to the screen, and then the content in the mode space will be deleted, At the same time, read the content matched by the next line into the pattern space and cycle in turn until the whole file is read.
- The sed command deals with the contents of the buffer. In fact, the buffer here is the pattern space
grammar
sed syntax supports two formats, namely
- sed [options] 'address command' file
- sed [options] -f [scripefile] file
Explain the above English characters:
- Options is options. The options supported by sed are described in the option summary below
- Address: equivalent to the matching condition, which will be described in the address
- Command is a command, which is equivalent to a parameter. It is placed after the address. For details, see the command section below
- File: is a text file
It should be noted that when used, address and command will be next to each other, and there is no space in the middle. The space in the middle of the above syntax is to illustrate that these are two parts
The following is the workflow diagram of sed command
option
The options here refer to the options after sed. The common options are as follows:
-e ,--expression=script As specified in the options script Parameter to process text files, you can specify multiple commands -f file,--files=script Processes a text file with the specified parameters in the file -n ,--quiet ,--slient Cancel default output, sed All text content will be output by default, using-n Only the processed rows are displayed after the parameter -r ,--regexp-extended Support extended regular expressions -i,--in-place[=SUFFIX] Directly modify file content
address
The address here is the address part in the syntax format, which is the first part in single quotation marks.
Replacement tag description linenumber specifies the specific line number startline,endline specifies the start line number and end line number linenumber,+nn is a number, which represents the matching pattern represented by regular expression n lines / pattern / from the specified line number / pattern1, /pattern2 / from pattern matching 1 to pattern matching 2pattern/,x query the line x containing the pattern on the given line number, /Pattern / query the matching rows x,y by line number and pattern! The query does not contain rows with the specified row numbers x and y
command
The instruction here is command, which is usually placed in single quotation marks and together with the character to be processed, but the command is placed in front of the character.
a Append, inserting content after the matching line i Insert, insert content before matching line c Change, change the content of the matching line d Delete, delete the content of the matching line s Replace the corresponding mode with the replacement mode s/patten/newstring Replace, put patten Replace the matched content with newstring p Print, print out the matching content, and-n Use with options = Label, which is used to label the matched line n Read next line, encountered n Automatically jumps to the next line r Read content into file {} The transfer between commands is similar to the pipe character| w Write matching content to file W Match to the first row of the row and save to file Yes.
Replace tag
- g: indicates that the matched content will be replaced globally.
- \1: Represents the content represented by the first left bracket in front, \ 2 represents the content represented by the second left bracket in front, and so on
- &: indicates the previous matching content
Regular matching
Matching has been described above. Please refer to regular expression introduction for details. For details, see 35 parameters and 13 cases to comprehensively analyze the grep command of the three swordsmen of Linux
example
The following practical exercises are mainly for address instructions
First prepare the data file AA Txt, view AA through cat Txt file content
[root@localhost test]# cat aa.txt Total consumption 28 lrwxrwxrwx. 1 root root 7 9 October 6:12 bin -> usr/bin dr-xr-xr-x. 6 root root 4096 9 October 6:44 boot drwxr-xr-x. 20 root root 3360 9 May 29:19 dev drwxr-xr-x. 3 root root 20 9 March 14:08 docker_registry drwxr-xr-x. 2 root root 41 9 June 17:23 docker_study drwxr-xr-x. 151 root root 12288 9 May 29:20 etc drwxr-xr-x. 3 root root 18 9 October 6:57 home lrwxrwxrwx. 1 root root 7 9 October 6:12 lib -> usr/lib lrwxrwxrwx. 1 root root 9 9 October 6:12 lib64 -> usr/lib64 drwxr-xr-x. 2 root root 6 4 November 2018 media drwxr-xr-x. 2 root root 21 9 September 19-21:51 mnt drwxr-xr-x. 4 root root 34 9 November 6:53 opt dr-xr-xr-x. 307 root root 0 9 May 29:19 proc dr-xr-x---. 7 root root 266 9 May 29:20 root drwxr-xr-x. 45 root root 1320 9 June 29:26 run lrwxrwxrwx. 1 root root 8 9 October 6:12 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 4 November 2018 srv dr-xr-xr-x. 13 root root 0 9 May 29:19 sys drwxr-xr-x. 3 root root 43 9 June 26-19:11 test drwxrwxrwt. 24 root root 4096 9 June 29:38 tmp drwxr-xr-x. 13 root root 155 9 October 6:12 usr drwxr-xr-x. 22 root root 4096 9 October 6:45 var
Displays the second line of the file
Delete 3 ~ 13 lines in the file
Delete rows matching boot
Delete lines starting with d
Delete lines containing the d character
Print lines starting with d to l (multiple matches)
Print dev to line 5, which does not contain line 5
Print lines from line 2 to match dev
Output other data except the data between 3 ~ 16 lines
The following are the practical exercises of regular matching
First, prepare the data file and view Java. Net through cat command Conf file
[root@localhost test]# [root@localhost test]# cat java.conf # System-wide Java configuration file -*- sh -*- # Location of jar files on the system JAVA_LIBDIR=/usr/share/java # Location of arch-specific jar files on the system JNI_LIBDIR=/usr/lib/java # Root of all JVM installations JVM_ROOT=/usr/lib/jvm # You can define a system-wide JVM root here if you're not using the # default one. # # If you have a base JRE package installed # (e.g. java-1.6.0-openjdk): #JAVA_HOME=$JVM_ROOT/jre # # If you have a devel JDK package installed # (e.g. java-1.6.0-openjdk-devel): #JAVA_HOME=$JVM_ROOT/java # Options to pass to the java interpreter #JAVACMD_OPTS= # You can disable ABRT Java Connector by setting JAVA_ABRT to "off". # See: https://github.com/jfilak/abrt-java-connector/ #JAVA_ABRT=off [root@localhost test]#
Filter out the comment line. Note: the comment line starts with #
Filter out all blank lines
Filter out all comment lines and empty lines. This command uses {}. For unclear lines, please refer to the above command
Delete all comment lines and blank lines (the deletion here does not delete the content of the source file)
Match any row that contains the JVM
Match contains JNI_LIBDIR or JVM_ Row of root
Match any rows that contain numbers
The following exercises are mainly for each command, such as s,i,a,r,w,p, etc
Replace the first character a containing aaa characters with b
Replace the characters containing aaa with bbb
Java_ Replace home with JAVA_DIR, notice that this file contains multiple lines of JAVA_HOME
Replace the root in the second line with test. Note that it is the first matched root character.
Replace the second, the root in the third line is test, and this time it is a global replacement
Replace the root from the second row to the last row with test (global replacement)
Add hello at the beginning of the line that matches the See character
On the line that matches the See character, add hello after the See character
On the line that matches the See character, add hello before the See character
On the line that matches the See character, add hello at the end of the line
Add hello at the beginning of each line.
Add hello at the end of each line.
Add hello at the beginning of the first three lines.
Add hello before the line that matches the ddd character
Add hello after the line that matches the ddd character
Add multiple lines of content next to the matching ddd line.
Add a new line to the third line of the file.
What line is the boot query in
All the above substitutions or changes will not change the contents of the original document.
Change ccc in the file info to xyz.
Change the line in the file info that matches tmp, including the character of root, to test
Add the end character at the end of the line that matches kkk.
Directly replace the contents of the third line in the file.
The specified file ABC Txt is inserted into the third line of the info file
Add file AA Txt to the text file ABC txt.
Add AA Txt to a1.txt Txt
Read the instructions in the file a.sh and replace xxx with hello world