How to use sed command to replace string under Linux

  
                

Understand the Linux system commands, the sed command is often used for the processing of text files. Today, Xiaobian will introduce you how to use the sed command to replace strings. Interested friends may wish to understand.

sed alternative basic syntax is:

code is as follows:

sed & lsquo; s /original string /replacement string /& rsquo;

Inside single quotes, s means replacement, the middle of the three diagonal lines is the replacement style, special characters need to use the backslash "\\” to escape, but the single quotes ”‘” there is no way to use the backslash Line & rdquo; \\ & rdquo; escaping, this time just change the single quotes in the command to double quotes, for example:

Code is as follows:

sed “s /original characters The string contains ‘/replace string contains ’/” //The characters to be processed contain single quotes

The three slash separators in the command can be replaced by other symbols, which are to be replaced It is more convenient to have more slashes, just follow s definition, for example, change to question mark ”? ”:

The code is as follows:

sed ‘s? Original string? Replace the string? ’ //Custom delimiter for question mark

You can replace each matching keyword with g at the end, otherwise replace only the first one of each line, for example:

The code is as follows:

sed ‘s/Original String/Replace String/’ //Replace all matching keywords

Up arrow”^” indicates the beginning of the line, USD”$&rdquo If the symbol indicates the end of the line in quotation marks, but the quotation mark indicates the last line (the last line), it is guilty of two, and the search for half a day which symbol indicates the first line, only half a day to think of it, the first line is the number & rdquo; 1 & Prime; what. Then adding a string at the beginning and end of the line is to replace the end of the line and the beginning of the line, for example:

The code is as follows:

sed & lsquo;s /^ /added head & /g’ //Add

sed ‘s/$/& at the beginning of all lines/g’ //Add

sed ‘2s/original string at the end of all lines /Replace string /g’ //Replace line 2

sed ‘$s/original string/replace string/g’ //Replace the last line

sed ‘2 , 5s /original string /replacement string /g & rsquo; //replace 2 to 5 lines

sed & lsquo; 2, $ s /original string /replacement string /g & rsquo; //replace 2 to the last One line

The substitution style can be executed in multiple commands in the same command, separated by a semicolon <;”, for example:

The code is as follows:

sed ‘s /^/Added header & /g;s/$/& added tail /g’ //Execute two replacement rules at the same time

The output processed by sed is output directly to the screen. To save, you can redirect the output, or use the parameters ”i”Replace directly in the file:

The code is as follows:

sed -i ‘s/original string/replace string/g’ filename //Replace all in the file Matches

The above is the way to replace strings with sed command under Linux. The usage of sed command is much more than this. This article just gives an example to give you a brief introduction to the sed command. You want to use the sed command to replace it. String friends can be a reference.

Copyright © Windows knowledge All Rights Reserved