Sed commands for file operations, sed commands for details
File Modification in a simple shell script in Linux
1. Introduction to Sed
Sed is an online editor that processes a row of content at a time. During processing, the currently processed rows are stored in the temporary buffer, called patternspace. Then, the sed command is used to process the content in the buffer. After processing, send the buffer content to the screen. Next, process the next row, and repeat until the end of the file. The file content is not changed unless you use the redirection storage to output the file. Sed is mainly used to automatically edit one or more files, simplify repeated operations on files, and write conversion programs.
2. Addressing
The address can be used to locate the row you want to edit. The address is composed of numbers, the two rows separated by commas indicate the range of rows starting and ending with the two rows (including the rows indicated by the number of rows ). For example, 1 and 3 indicate 1, 2, 3 rows, and the dollar sign ($) indicates the last row. The range can be determined by data, regular expressions, or a combination of the two.
3. Sed Command Format
The sed command can be called in two forms:
Sed [options] 'command' file (s)
Sed [options]-fscriptfilefile (s)
4. Common Sed commands
A
Add a line of text behind the current line.
Blable
Branch to the marked place in the script. If the branch does not exist, branch to the end of the script.
C
Use new text to change the text of this line.
D
Delete a row from the position of the template block (Patternspace.
D
Delete the first line of the template block.
I
Insert text on the current row.
H
Copy the content of the template block to the buffer in the memory.
H
Append the content of the template block to the buffer in the memory.
G
Obtain the content of the memory buffer and replace the text in the current template block.
G
Obtain the content of the memory buffer and append it to the end of the current template block text.
L
List of characters that cannot be printed.
N
Read the next input line and use the next command to process the new line instead of the first command.
N
Append the next input row to the end of the template block and embed a new line between them to change the number of the current row.
P
Print the row of the template block.
P (uppercase)
Print the first line of the template block.
Q
Exit Sed.
Rfile
Read rows from the file.
Tlabel
If branch, starting from the last line, once the condition is met or the T, t command will lead to branch to the command with a label or to the end of the script.
Tlabel
An error branch starts from the last line. If an error occurs or the T or t command, the Branch is routed to the command with a label or to the end of the script.
Wfile
Write and append the template block to the end of the file.
Wfile
Write and append the first row of the template block to the end of the file.
!
Indicates that the subsequent commands will work on all unselected lines.
S/re/string
Use string to replace the regular expression re.
=
Print the number of the current row.
#
Extend the annotation to the next line break.
Replace tags
G indicates that all rows are replaced.
P indicates printing rows.
W indicates writing rows into a file.
X indicates the text in the SWAp template block and the text in the buffer.
Y indicates translating a character into another character (but not used in regular expressions)
5. Command Options
-Ecommand, -- expression_r = command
Multiple edits are allowed.
-H, -- help
Print help and display the address of the bug list.
-N, -- quiet, -- silent
Cancel default output.
-F, -- filer = script-file
The name of the bootstrap sed script file.
-V, -- version
Print the version and copyright information.
6. metacharacters
^
The start of the anchor row is as follows:/^ sed/matches all rows starting with sed.
$
The end of the anchor row is as follows:/sed $/matches all rows ending with sed.
.
Match a non-linefeed character, for example,/s. d/match s, followed by any character, and then d.
*
Match zero or multiple characters, such as:, love, which is ** love **.
<
Anchor specifies the start of a word, for example,/<love/matches a line that contains a word starting with love.
>
Anchor specifies the end of a word, for example,/love>/matches a row containing a word ending with love.
X {m}
Repeat the characters x and m, for example,/0 {5}/matches the rows containing 5 o.
X {m ,}
Repeated characters x, at least m times, such as:/o {5,}/matching rows with at least 5 o.
X {m, n}
Repeated characters x, at least m times, no more than n times, such as:/o {5, 10}/rows matching 5-10 o.
7. Examples
Delete: d command
$ Sed '2d 'example ----- Delete the second line of the example file.
$ Sed '2, $ d' example ----- delete all rows from the second row to the end of the example file.
$ Sed '$ d' example ----- Delete the last row of the example file.
$ Sed '/test/' dexample ----- delete all rows containing test in the example file.
Replace: s command
$ Sed's/test/mytest/G' example ----- replace test with mytest in the entire row. If no g tag exists, only the first matched test in each row is replaced with mytest.
$ Sed-n's/^ test/mytest/p 'example ----- (-n) option and the p Flag are used together to print only the replaced rows. That is to say, if the test at the beginning of a row is replaced with mytest, print it.
$ Sed's/^ 192.168.0.1/& localhost/'example ----- & symbol indicates replacing the part found in the string. All rows starting with 192.168.0.1 are replaced with their own localhost and changed to 192.168.0.1localhost.
$ Sed-n's/(love) able/1rs/P' example ----- love is marked as 1, all loveable will be replaced with lovers, and the replaced lines will be printed out.
$ Sed's #10 #100 # g'example ----- whatever the character, followed by the s command is considered as a new separator. Therefore, "#" is a separator here, replaces the default "/" separator. Replace all 10 with 100.
Range of selected rows: comma
$ Sed-n'/test/,/check/P' example ----- all rows within the range specified by the template test and check are printed.
$ Sed-n' 5,/^ test/P' example ----- print all rows starting from the fifth line to the first line that contains the beginning of test.
$ Sed '/test/,/check/s/$/sedtest/'example ----- for the rows between the template test and west, the end of each row is replaced by the string sedtest.
Multi-Point Editing: e command
$ Sed-e '1, 5d '-e's/test/check/'example ----- (-e) options allow multiple commands to be executed in the same line. As shown in the example, the First Command deletes lines 1 to 5, and the second command replaces test with check. The command execution sequence has an impact on the result. If both commands are replacement commands, the first replacement command will affect the result of the second replacement command.
$ Sed -- expression ='s/test/check/'-- expression ='/love/d' example ----- a better command than-e is -- expression. It can assign values to sed expressions.
Read from file: r command
$ Sed '/test/rfile' example ----- the content in file is read and displayed after the row matching test. If multiple rows match, the file content is displayed under all matched rows.
Write File: w command
$ Sed-n'/test/wfile' example ----- all rows containing test in example are written to file.
APPEND Command: command
$ Sed '/^ test/a \ thisaexample 'example ----- 'thisaexample' is appended to the line starting with test. sed requires that command a be followed by a backslash.
Insert: I command
Sed '/test/I \ abcde' example ------- abcde is inserted before the row containing test.
Next: n command
$ Sed '/test/{n; s/aa/bb/;}' example ----- if test is matched, move it to the next row of the matched row and replace aa of this row, change to bb, print the row, and continue.
Deformation: y command
$ Sed '1, 10y/abcde/ABCDE/'example ----- converts all abcde in line 1-10 to uppercase. Note that this command is not applicable to the regular expression metacharacters. That is, the length of the string to be replaced must be the same as that of the string to be replaced.
Exit: q command
$ Sed '10q' example ----- exit sed after printing the 10th rows.
Maintain and obtain: h and G commands
$ Sed-e '/test/H'-e' $ G' example ----- when sed processes files, each row is saved in a temporary buffer called a mode space, all processed rows are printed on the screen unless the row is deleted or the output is canceled. Then, the empty dialog box is cleared, and a new row is saved for processing. In this example, the row Matching test is found and saved to the mode space. The h Command copies the row and saves it to a special buffer zone called keep cache. The second statement means that when the last line is reached, the G command extracts the row that maintains the buffer and places it back in the mode space, and append it to the end of the row that already exists in the mode space. In this example, It is appended to the last row. To put it simply, if the H command is used, any line containing the test will be copied and appended to the end of the file. If the h command is used, only the last row containing test is copied and appended to the end of the file.
Persistence and interchange: h and x commands
$ Sed-e '/test/H'-e'/check/X' example ----- swap mode space and keep the buffer content. That is, replace the rows that contain check with the rows that contain test, instead of swapping the two rows.
7. Script
Sed script is a list of sed commands. When Sed is started, the file name of the script is guided by the-f option. Sed is very picky about the commands entered in the script. There cannot be any blank or text at the end of the command. If there are multiple commands in one line, separate them with semicolons. Comments rows starting with # And cannot span rows.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.