linux--Find and replace

Source: Internet
Author: User

linux--Find and replace

Text lookup:
grep, Egrep, Fgrep

grep: Searches a document based on a pattern defined by a basic regular expression and displays a line of text that conforms to the pattern
Note: Search is part of the search, as long as a line has a certain part of the matching mode, it will be displayed
Patterns: pattern, text word wildcards regular the matching criteria of the expression's meta-character combination
grep option Mode file
-I: Ignore case
[Email protected]:~/linlin/test$ cat Exp.txt
I have a pen
I like to eat apple
The Hhhhh
The Hhhhh
[Email protected]:~/linlin/test$ grep-i HHH exp.txt
The Hhhhh
The Hhhhh
--color: Highlighting
-V: Show rows that are not matched by pattern
[Email protected]:~/linlin/test$ grep-v HHH exp.txt
I have a pen
I like to eat apple
The Hhhhh
-O: Displays only strings that are matched by the pattern
[Email protected]:~/linlin/test$ grep-o HHH exp.txt
Hhh
-e: Extend regular expression, equal to Egrep
-F: Match with a fixed string equal to Fgrep

Egrep: Equals grep-e

Fgrep: Regular expression is not supported, but search is faster

Text substitution
Sed: stream editor, editing a file in batches rather than interactively.
It is a very useful tool in text processing and can be used perfectly with regular expressions. When processing, stores the currently processed rows
In the temporary buffer, called the pattern space, and then uses the SED command to process the contents of the buffer, where
The contents of the buffer are sent to the screen after the manager completes. Then the next line is processed, so it repeats until the end of the file. In the file
No change in capacity. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.

Mode space: In memory

Syntax format: sed [option] ' address (which rows are processed) command ' FILE (s)

Common options:
-N: Silent mode, no longer displays content in mode space by default
-I: Modify the original file directly
-e Script-e script ... : Using Multiple scripts
-F/File/to/script/: Specify a file, each line of the file is a script (address, command)
-R: Using extended regular expressions

Said:
1. Address: Start line, end line
For example, 1,100
2. Commands: Patterns specified with regular expressions
3. Mode 1, pattern 2 means that the first time the line is matched by pattern 1 to the end of the first line that is matched by pattern 2, all rows in the middle
4. Number of rows, processing the specified row
$ represents the last line, $-n represents the penultimate line n+1
5. Starting line, +n
Represents a total of n+1 rows from the starting line to the back n rows

Command:
A\ inserts text below the current line.
I\ inserts text above the current line.
C\ changes the selected line to a new text.
d Delete, delete the selected row.
D Delete the first line of the template block.
s replaces the specified character
H copies the contents of the template block into an in-memory buffer.
H appends the contents of the template block to the in-memory buffer.
G Gets the contents of the memory buffer and overrides the text in the current template block.
G gets the contents of the memory buffer and appends it to the text of the current template block.
The list of characters cannot be printed.
N reads the next input line, processing the new row with the next command instead of the first command.
N appends the next input line to the template block and embeds a new line between them, changing the current line number.
P Prints the line of the template block.
P (uppercase) prints the first line of the template block.
Q Exit sed.
b lable branches to the markup in the script, branching to the end of the script if the branch does not exist.
R file reads rows from file.
The T-label if branch, starting from the last row, will cause the branch to be at the command with a label, or to the end of the script, once the condition satisfies or t,t the command.
The T label Error branch, starting with the last line, will cause the branch to be at the command with a label, or at the end of the script, once an error or T,T command occurs.
W file writes and appends the template block to the end of file.
W file writes and appends the first line of the template block to the end of file.
! Indicates that the subsequent command has effect on all rows that are not selected.
= Prints the current line number.
# extend annotations before the next line break.

s/mode/string/modifier: Find and Replace, convert a string in each row to a specified string, and by default only replaces the first string in each row that is matched to the pattern
Modifier
G: Global Substitution
I: Ignore character case
The delimiter after s does not necessarily need to be \ or other content, but each delimiter needs to be the same, which guarantees that the text content contains delimiter characters
s\\\,s###
\ (\), \1,\2 ... Back to reference
&amp: The entire string that the reference pattern matches to, that is, what the pattern matches to
The-n option is used with the P command to display only the rows that match
[Email protected]:/home/linlin/linlin/shell# sed-n ' S/ROOT/ROOT/PG '/etc/passwd
Root:x:0:0:root:/root:/bin/bash


linux--Find and replace

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.