The principle of doing things enough, or endless, simple use of the grep command and sed command.
1, grep
Grammar:
grep pattern filename
Pattern (search for strings, numbers, etc., you can also use positive expressions, wildcard characters, and so on)
FileName (File/directory or.)
Parameters
Parameters |
function |
- I. |
Ignore Case |
-W |
Whole Word match |
-r/r |
recursively match files in a folder |
- N |
Show Line Numbers |
-C |
Show number of matching rows |
-V |
Show lines that do not match |
- L |
display a matching filename |
2. SED command
(1), the introduction of the order
Sed is a good file processing tool , itself is a pipe command, mainly in the behavior of the unit to deal with, you can replace, delete, add, select
For specific tasks, let's take a look at the use of SED
The SED command line format is:
sed [-nefri] ' command ' Enter text
(2), parameter
Common options:
-N: Use Quiet (silent) mode. In general sed usage, all data from stdin is generally listed on the screen.
However, if you add the- n parameter, only the line (or action) that is specifically handled by SED will be listed .
-E: SED action edit directly on instruction column mode;
-F: Directly write the action of SED in a file, the-f filename can perform the SED action in filename;
-r:sed's actions support the syntax of extended formal notation. (Presupposition is the basic formal representation of French law)
-I: Directly modify the contents of the file read, not by the screen output.
Common commands:
A: New, a can be followed by a string, and these strings will appear on the new line (the current line) ~
C: Replace, C can be followed by strings, these strings can replace the line between n1,n2.
d: Delete , because it is deleted ah, so d usually do not pick up any of the drums;
I: Insert, I can be followed by strings, and these strings will appear on a new line (the current line);
p: Print, that is, to print out a selection of data. Normally p will work with the parameter Sed-n ~
S: Content replacement.
(3), instance (file name is test)
(1) Delete a row
Sed ' 1d ' test #删除第一行
sed ' $d ' test #删除最后一行
sed ' 1,2d ' test #删除第一行到第二行
sed ' 2, $d ' Test #删除第二行到最后一行 <span style= "Font-family:simsun; Background-color:rgb (255, 255, 255); > </span>
Delete a matching row
Sed-i '/Match string/d ' filename (Note: If the matching string is a variable, you need "" instead of "")
(2) Display a row
Sed-n ' 1p ' test #显示第一行
sed-n ' $p ' test #显示最后一行
sed-n ' 1,2p ' test #显示第一行到第二行
sed-n ' 2, $p ' test< c8/> #显示第二行到最后一行
(3) Use mode to query
Sed-n '/ruby/p ' test #查询包括关键字ruby所在所有行
sed-n '/\$/p ' Test #查询包括关键字 $ all rows, using backslashes \ Masking special meaning
(4) Adding one or more lines of string
Sed ' 1a drink tea ' test #第一行后增加字符串 "Drink tea"
sed ' 1,3a drink tea ' test #第一行到第三行后增加字符串 "drink tea"
sed ' 1a D Rink Tea\nor Coffee ' test #第一行后增加多行, using line breaks \ n
(5) Instead of one or more lines
SED ' 1c hi ' test #第一行代替为Hi
sed ' 1,2c hi ' Test #第一行到第二行代替为Hi
(6) Replacing a part of a row
Format: sed ' s/the string to be replaced/the new string/g ' (the string to be replaced can be in regular expression)
sed-n '/ruby/p ' test | sed ' s/ruby/bird/g ' #替换ruby为bird
sed-n '/ruby/p ' test | sed ' s/ruby//g ' #删除ruby
(7) Replace a string in a matching row
Sed-i '/matching string/s/replacement source string/replacement destination string/g ' filename
(8) inserting
Sed-i ' $a bye ' ab #在文件ab中最后一行直接输入 "Bye"
That's about it, so use it to find something else.