SED is a good file processing tool. It is a pipe order command, which is mainly processed by the behavior unit. You can replace, delete, add, select, and select specific tasks. Let ’s take a look at the usage of SED first.
SED command line format is:
SED [-Nefri] ‘Command’ Input text
-
Common options:
-N: Use Silent mode. In general SED usage, all data from STDIN are generally listed on the screen. But if the -N parameters are added, the line (or action) that is specially processed by SED will be listed. -E: SED action editing directly on the instruction column mode; -F: Write SED's action directly in a file, -F FILENAME can perform the SED action in Filename; -R: SED's action supports the syntax of extended formal representation. (The preset is the basic formal expression of the law) -I: The content of the read -readable file is directly modified, not the output of the screen.
-
Commonly used commands:
A: Newly increased, the latter of A can be connected, and these string will appear in the new line (the current next line) ~ C: Replacement, you can connect the string behind C, these string can replace the line between N1, N2! D: Delete, because it is deleted, so there is usually no puppet behind D; I: Insert, you can connect the string behind i, and these string will appear in the new line (the current last line); P: Print, it is also printed by a certain choice. Usually P will operate with the parameter SED -N ~ S: Replacement, you can directly replace work miles! Usually this S movement can be matched with formal expressions! For example, 1,20S/OLD/New/G is just!
-
Example: (Suppose we have a file named AB)
Delete a certain line
[[email protected]dgj] # SED ‘1D’ ab # delete the first line
[[email protected] dgj] # sed ‘Katex PARSE ERROR: Expected ‘EOF’, GOT ‘#At Position 20:… B#̲ Delete the last line [root …d ’ab # delete the second line to the last line
Show a line
. [[email protected]dgj] # SED -N ‘1p’ ab # display the first line
[[email protected] dgj] # sed -n ‘Katex PARSE ERROR: Expected ‘EOF’, Got ‘#At Position 17: … AB#̲ Show the last line [Ro …p ‘ab # display the second line to the last line
Use mode to query
[[email protected]dgj] # SED -N '/Ruby/P' AB # Query includes all walks where the keyword Ruby is located
[[email protected]dgj] # SED -N ‘/$/p’ ab # Query includes all the lines where keywords are located.
Add a line of line or multi -line string `
[[email protected] dgj]# cat ab
Hello!
dgj is me,welcome to my blog.
end
[[email protected]dgj] # SED '1A DRINK Tea' Ab # After the first line, add the string "Drink Tea"
Hello!
drink tea
dgj is me,welcome to my blog.
end
[[email protected]dgj] # SED '1,3A Drink Tea' AB # first line to the third line to add string "Drink Tea"
Hello!
drink tea
dgj is me,welcome to my blog.
drink tea
end
drink tea
[[email protected]dgj] # SED '1A DRINK Tea \ NOR COFFEE' AB # first row to add more lines, use the change symbol \ n
Hello!
drink tea
or coffee
dgj is me,welcome to my blog.
end
Instead of one line or multiple lines
[[email protected]dgj] # SED '1c Hi' ab # first line instead of Hi
Hi
dgj is me,welcome to my blog.
end
[[email protected]dgj] # SED '1,2C Hi' ab # first line to the second line instead of Hi
Hi
end
replace a part of one line
Format: SED ‘S/Strings to replace/new string/g (new string to be replaced can be used in regular expression)
[email protected]dgj] # SED -N ‘/Ruby/P’ AB | SED ’s/Ruby/Bird/G’ #
[[email protected]dgj] # SED -N ‘/Ruby/P’ AB | SED ’s/Ruby // G’ # delete Ruby
Insert[email protected]dgj] # SED -i '$ a bye' ab # in the last line in file AB, enter "bye"[email protected]dgj]# Cat AB
Hello!
dgj is me, welcome to my blog.
end
bye
Delete matching
SED -i '/matching string/d'filename (Note: If the matching string is a variable, you need "" instead of ‘’. Remember)
Replace a string in the matching match
SED -i '/matching string/s/replacement source string/replacement target string/g' filename