Text editing for Linux diff & Sed & awk & grep

Source: Internet
Author: User
Tags diff print format

grep (keyword: intercept) text-gathering tool, combined with regular expression is very powerful
Main parameters []
-C: Output only matching rows
-I: Case insensitive
-H: Do not display file names when querying multiple files
-L: Only file names with matching characters are output when querying multiple files
-N: Displays matching line numbers and rows
-V: Displays all lines that do not contain matching text (I often use the remove grep itself)
Basic how to work: grep to match the content file name, for example:
grep ' test ' d* shows all rows with test in the file beginning with D
grep ' Test ' AA bb cc shows lines that contain test in the AA bb cc file
grep ' [a-z]\{5}\ ' AA displays all strings containing a string of at least 5 consecutive lowercase letters




awk (keyword: analysis & processing)Parsing of one line of processing awk ' condition type 1{action 1} condition type 2{action 2} ' filename, awk can also read standard input from the previous instruction
Unlike SED, which is often used for a whole line of processing, awk prefers to divide a row into several "fields" (areas), with the default delimiter being the SPACEBAR or TAB key
For example:
Last-n 5 | awk ' {print ' \ t ' $ ' $} ' there is no space between "\ t" in curly braces here, but it is best to add a space, and note that "\ T" is double-quoted, because the contents are in single quotes
$ A represents the entire row, representing the first region, and so on
The processing process for awk is:
1. Read the first line and fill in the first line with the variable ... In equal variables
2. Perform the action according to the condition limit
3. Next line of execution
As a result, awk is processed one line at a time, and the smallest unit processed at a time is a region
There are also 3 variables, NF: The number of fields processed per row, NR currently processed to the first few lines of FS current delimiter
Logic judgment > < >= <= = =!==, assignment direct use =
cat/etc/passwd | awk ' {fs= ': '} $3<10 {print ' \ t ' $ ' $} ' first defines the delimiter as:, then judge, watch, judge not write in {}, then perform the action, fs= ":" This is an action, assignment action, not a judgment, so do not write in {}
Begin END, which gives the programmer an initialization and finishing work, the actions listed after begin will be executed within {} Before awk begins scanning the input, and the actions within end{} will be executed after the input file has been scanned.
awk '/test/{print NR} ' ABC prints the line number of the line with test, note that regular expressions can be used between//
Within awk {}, you can use if else, for (i=0;i<10;i++), i=1 while (I&LT;NF)
VisibleMany of Awk's uses are equivalent to the C language, such as "\ T" delimiter, print format, if, while, and so on

Awk is a fairly complex tool, and when it's really used, add it. (Picture of the tool)



sed (keyword: edit) text Editing tool with behavioral units sed can directly modify the file, but it is generally not recommended to do so, you can analyze the standard input
Basic working methods: sed [-NEF] ' [action] ' [input text]
-N: Quiet mode, in general SED usage, data from stdin is generally listed on the screen, and if the-n parameter is used, only the line that is processed by SED is listed.
-E: Multiple edits, such as you want to delete a row at the same time, and want to change the other rows, then you can use Sed-e ' 1,5d '-e ' s/abc/xxx/g ' filename
-F: First write the action of SED in a file, and then through the Sed-f ScriptFile can directly perform the SED action within the ScriptFile (no experimental success, not recommended)
-I: Direct editing, this time is really changing the contents of the file, and everything else just change the display. (Not recommended)
Action:
A is added, a string is followed by a, and the string appears on a new line. (Next line)
C supersedes, C-strings, which can replace rows between n1,n2
D Delete, not after anything.
I insert, followed by a string that appears on the previous line
P prints, lists selected data, usually works with Sed-n sed-n ' 3p ' only prints line 3
s substitution, similar to the substitution in VI, 1,20s/old/new/g

[Line-address]q exit, match to a line to exit, improve efficiency

[Line-address]r matches to a line to read a file for example: sed ' 1r qqq ' ABC, note that the written text is written on the 1th line behind, that is, line 2nd

[Line-address]w file, the line to which the match is written to a file such as: Sed-n '/m/w QQQ ' ABC, reads the line with M from ABC to the QQQ file, note that this write has coverage.


Text editing for Linux diff & Sed & awk & grep

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.