This section will use the Sample1.txt file as an example, and the contents of the file are as follows: some fruits. :
Apple
Orange
Banana
Pappaya
1. Add content to the beginning of each line, here we add "Fruit:"
$ sed ' s/^/fruit:/' Sample1.txt
Fruit:apple
Fruit:orange
Fruit:banana
Fruit:pappaya
Parsing: s represents substitution, which is substitution, S is to replace/match the content, slash/use to separate s and the original content to be replaced and the final content to replace, and the ' ^ ' symbol is to say a regular, to match the beginning of each line, match success at the beginning with ' Fruit: '.
2. Add content to the end of each line
$ sed ' s/$/fruit/' sample1.txt
Apple Fruit
Orange Fruit
Banana Fruit
Pappaya Fruit
Note that here the $ and the $ symbol in the previous section mean different meanings, and here is the end of the line.
3. How to replace the specified character, where lowercase a is replaced with uppercase a
$ sed ' s/a/a/' sample1.txt
Apple
OrAnge
BAnana
Pappaya
Note that only the first a in each row is replaced, not all, this example means replacing a single character, you can replace a word is OK.
4. How to replace all the characters in the line, replace a with a
$ sed ' s/a/a/g ' sample1.txt
Apple
OrAnge
BAnAnA
Pappaya
Note, just add a g option, g for global shorthand, is the global, all meaning.
5. How to replace the second occurrence of a?
$ sed ' s/a/a/2 ' sample1.txt
Apple
Orange
BanAna
Pappaya
Not using G, but using numbers to represent the first occurrence of a in the line, the result
6. How do I replace all a after the second time?
$ sed ' s/a/a/2g ' sample1.txt
Apple
Orange
BanAnA
Pappaya
Very well understood, right.
7. What if you only want to replace the third row of a?
$ sed ' 3s/a/a/g ' sample1.txt
Apple
Orange
BAnAnA
Pappaya
Recall that the first section, before executing the command, determines whether the current address satisfies the condition, and 3 is the location
8. Want to replace the data in a range of rows?
$ sed ' 1,3s/a/a/g ' sample1.txt
Apple
OrAnge
BAnAnA
Pappaya
Separated by commas, you can
9. How do I replace the whole line? For example, replace Apple with the apple is a fruit
$ sed ' s/./& is a fruit/' sample1.txt
Apple is a Fruit
Orange is a Fruit
Banana is a Fruit
Pappaya is a Fruit
Here the ' & ' symbol identifies what the pattern matches to, and. Matches a positive line,. Represents any character, * represents one or more, that is, it matches the entire row,& so it is the entire line, and is useful for renaming a group of files.
10. How to make multiple substitutions, such as replacing a with a, replacing p with P
$ sed ' s/a/a/g; S/p/p/g ' Sample1.txt
APPle
OrAnge
BAnAnA
Pappaya
That is, separated by semicolons. Or you can do it with the-e argument.
$ Sed-e ' s/a/a/g '-e ' s/p/p/g ' sample1.txt
APPle
OrAnge
BAnAnA
Pappaya
The-e option is used when multiple substitutions are required.
In addition, if there are too many replacements. You can also break into multiple lines by right slash.
$ Sed-e ' s/a/a/g ' >-e ' s/p/p/g ' sample1.txt
APPle
OrAnge
BAnAnA
Pappaya
Believe that through the example of this article, the usual file content replacement is very simple