Several ways to replace a string with Linux

Source: Internet
Author: User

1. Basic replacement
: s/str1/str2/replaces the current line the first str1 is str2
: S/str1/str2/g replaces the current line with all str1 as str2
: N, $s/str1/str2/replaces the first str1 of each row in the beginning of the nth row to the last row is str2
: N, $s/str1/str2/g replace the nth line to the last row, and all str1 are str2 in each row
(n is a number, if N is., representing the beginning of the current line to the last row)
:%s/str1/str2/(equivalent to: g/str1/s//str2/) replaces the first str1 of each line as str2
:%s/str1/str2/g (equivalent to: g/str1/s//str2/g) replaces all str1 in each row as str2
2. You can use # as a delimiter, at which time the middle appears/does not act as a delimiter
: s#str1/#str2/# Replace the current row the first str1/is str2/
:%s+/str11/str12/+/str21/str22+ (use + to replace/):/str11/str12/Replace with/str21/str22/
3. Delete ^m in file
Problem description: For line breaks, Windows uses a carriage return (0a0d) to indicate that Linux is a carriage return (0A). That way, when you copy a file on a window to UNIX, there's always a ^m, write a shell or C program that uses a newline character (0D) that filters Windows files under UNIX. There are two ways to do this:
(1) using the command: Cat Oldfilename | tr-d "^v^m" > NewFileName
(2) Use command: Sed-e "s/^v^m//" Oldfilename > NewFileName
It is important to note that in 1, 22 methods, ^v and ^m refer to Ctrl + V and ctrl+m. You have to do the input manually, not paste it.
Processing in VIM: First use VIM to open the file, then press ESC, then enter the command:
:%s/^v^m//
:%s/^m$//g
If the above method is useless, the correct solution is:
(1) tr-d "\ r" < Old_file_name > New_file_name or cat oldfilename | Tr-d "\ r" > New_file_name
(2) tr-d "\015" < Old_file_name > New_file_name or cat oldfilename | tr-d "\015" > New_file_name
Strings A>b
4. Other tricks
Use the: s command to implement a string substitution. Specific uses include:
: s/str1/str2/replaces the first occurrence of a string in a line with a string str2 str1
: S/str1/str2/g replaces all occurrences of strings in a line with a string str2 str1
:., $ s/str1/str2/g Replace all occurrences of the current line to the end of the string with the string str2 str1
: 1,$ s/str1/str2/g replaces all occurrences of strings in the body with a string str2 str1
: g/str1/s//str2/g function Ibid.
As you can see from the Replace command above, G is placed at the end of the command to replace each occurrence of the search string, without G, to replace only the first occurrence of the search string, and g at the beginning of the command to replace all rows in the body that contain the search string.
(2) SED command replacement string
The basic syntax for sed substitution is:
The code is as follows:

Sed
' s/original string/replace string/'
Single quotes inside, s for substitution, three slash in the middle is a replacement style, special characters need to be escaped with a backslash "\", but the single quote "'" is no way to escape with a backslash "\", this time as long as the single quotation mark in the command is changed to double quotation marks on the line, for example:

The code is as follows:

Sed "s/original string contains '/substitution string contains '/'
The character to be processed contains single quotation marks
The three slash delimiter in the command can be replaced by a different symbol, which is more convenient to replace with more slashes, just follow the s definition, such as the question mark "?":

The code is as follows:

Sed ' s? original string? Replace String? '
A custom delimiter is a question mark
You can replace each matched keyword with g at the end, or replace only the first of each line, for example:
The code is as follows:

Sed ' s/original string/replace string/'
Replace all matching keywords
Up ARROW "^" means the beginning of the line, the dollar "$" symbol if the end of the lines in quotation marks, but in the introduction of the last row (the final line), here committed two, searched for half a day which symbol represents the first line, half a day just remembered, the first line is the number "1″ ah." Then adding a string at :

The code is as follows:

Sed ' s/^/added head &/g '//Added at all beginning of line
Sed
' s/$/& added tail/g '//added at the end of all lines
Sed ' 2s/original string/replacement string/g '//Replace line 2nd
Sed
' $s/original string/replace string/g '//replace last line
Sed ' 2,5s/original string/replacement string/g '//replace 2 to 5 lines
Sed
' 2, $s/original string/replace string/g '//replace 2 to last line
A replacement style can be executed in more than one command, with a semicolon ";" Separation, for example:

The code is as follows:

Sed ' s/^/added header &/g;s/$/& added tail/g '
Execute two substitution rules at a time
The output processed by SED is output directly to the screen, to be saved can redirect the output, or replace it with the parameter "I" directly in the file:

The code is as follows:

Sed-i ' s/original string/replacement string/g ' filename
Replace all occurrences in a file

Several ways to replace a string with Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.