grep [option] Pattern [FILE ...]
-I: Ignore case
--colour: Display in a highlighted color
-V: Show rows that are not matched to
-O: Displays only the string that is matched to
-E: Using extended regular expressions
-A #: including how many lines later
-B #: including how many lines ahead
-C #: including how many lines before and after
To extend the regular expression:
Character Matching:
.
[] such as: a-z,a-z
[^]
Number of matches:
*: Any time before the character
? : matches the preceding character 1 or 0 times
+: Match the characters in front of it at least once
{M,n}:
Location anchoring:
^: Anchor the beginning of the line, any content after this character must appear at the beginning of the line
$: Anchor line end, any content after this character must appear at the beginning of the
^$: Blank Line
\< or \b: Anchor word, any character following it must appear as the first word
\> or \b: anchors the ending, any character preceding it must appear as the front of the word
Basic usage of SED;
Sed: line Editor
Sed ' addresscommand ' file ....
-N: Silent mode, content that is not in the default display mode space
-I: Modify the original file directly
-E script-e script: Multiple scripts can be executed simultaneously
-f/path/to/sed_script:
-r: Indicates the use of extended regular expressions
Command
D
P
A \string
I \string
R filename
W filename
s/pattern/string/modifier:
G: Global Substitution
I: Ignore case when finding
Sed exercises:
1, delete the/etc/grub.conf file in the beginning of the blank character;
Sed-r ' [email protected]^[[:space:]][email protected]@g '/etc/grub.conf
2. Replace the number in the "Id:3:initdefault:" line in the/etc/inittab file with 5;
Sed ' [email protected]\ (id:\) [0-9]\ (: initdefault:\) @\15\[email protected] '/etc/inittab
3, delete the blank line in the/etc/inittab file;
Sed '/^$/d '/etc/inittab
4. Delete the # number at the beginning of the/etc/inittab file;
Sed ' s/^#//g '/etc/inittab
5. Delete the # number at the beginning of a file, but require a white-space character after the # number;
Sed-r ' [email protected]^#[[:space:]][email protected]@g '/etc/inittab
6. Delete the white space characters in the file with the white space followed by the # in the convoy and #
Sed-r ' [Email protected]^[[:space:]]+#@@g '/etc/inittab
7. Take out a directory name for a file path;
echo "/ETC/RC.D" | Sed-r ' [email protected]^ (/.*/) [^/]+/[email protected]\[email protected] '
This article is from "Satan Daily" blog, please make sure to keep this source http://satantiantian.blog.51cto.com/6159537/1569246
Linux Note three: usage of grep and SED