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.
Using Docker on a Linux system can repeatedly deploy software in an environment, which makes it poss
When using a computer, we hope to recover important files that have been accidental
On Linux systems, if you want to expand disk capacity, you can create new virtual disk space. After
The Linux pwd command is mainly used to print the working path of the current working directory. If
How to import files from a Docker container to the host
Linux ssh connection prompts The authenticity of how to remove?
Linux command shutdown restart command usage summary
Linux software raid operation guide
How to use Autofs to mount files in RHEL5
Two styles of chmod command format under Linux/Ubuntu
Linux method for copying files/folders using CP commands
CentOS system implements SSH passwordless login method
What should I do if Linux vi is stuck?
Linux system Rhythmbox player how to solve Chinese garbled?
What should I do if Win10 forces an update? Microsoft released new tools
Win8 system update device driver tutorial
Win7 desktop wallpaper how to automatically change
Automatically run the program, win7 mission plan to help you
Gmail's Foxmail settings Foxmail set up Gmail tutorial
Windows 7 universal shortcuts and slimming optimization strategy
Graphical tutorial for adding download options in the Windows 7 Start menu