Shell's three large text processing tools grep, sed, and awk

Source: Internet
Author: User

grep, sed, and awk are all text processing tools, and although they all have their advantages and disadvantages, one text processing command cannot be completely replaced by another, or three text-processing commands will not appear. In comparison, however, SED and awk are more powerful and have been introduced in a separate language.

grep: Text filter, if just filter text, can use grep, its efficiency is much higher than other;

Sed:stream Editor, Stream editor, default only processing mode space, not processing the original data , if you process the data is processed for the row, you can use SED;

awk: Report generator, formatted for display later. If you need to generate information about the data you are working on, or if the data you are working on is column-based, you might want to use awk.

Grep

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

The above has already made the explanation

Http://www.cnblogs.com/-zyj/p/5760484.html

Sed

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]
A\: Adds one or more rows after the current line. Multiple rows, except for the last row, need "\" to continue at the end of each line
C\: Replaces the text in the current line with the new text after this symbol. Multiple rows, except the last line, need "\" at the end of eachCont.
I\: Inserts text before the current line. Rows are deleted at the end of each line, except for the last row, when multiple rows are added
H: Copy the contents of the pattern space to the staging buffer
H: Append the contents of the pattern space to the staging buffer
G: Copy the contents of the staging buffer to the schema space, overwriting the original content
G: Append the contents of the staging buffer to the schema space, appended to the original content
  L: List nonprinting characters
   P: Print line
  Q: End or exit sed
  r: Read input line from file
  ; ! : Apply the command
  S: replace one string with another
    G: Global substitution in line
    W: Write the selected lines to a file
  y: replace the character with another character (you cannot use the Y command for regular expressions)
options
-E: Multiple edits, that is, when applying multiple SED commands to an input line
-N: Cancels the default output
-F: Specifies the file name of the sed script

< Span style= "FONT-SIZE:16PX;" > example:

Awk

When sed deals with documents in an Act unit, awk is stronger than sed in that it can be handled not only by the Unit of action but also by the unit . The default line delimiter for awk is line breaks, and the column separators that are not saved are contiguous spaces and tabs .

However, the row and column delimiters can be customized, for example, each row of the/etc/passwd file has a dry field, and the fields are separated by: to redefine the column delimiter of awk as: and to process the file as a unit.

awk is actually a very complex scripting language, and there are branching and looping constructs like the C language , but the basic syntax is similar to SED, and the basic form of the awk command line is :

  awk option ' script ' file1 file2 ...?

awk option-f scriptfile file1 file2 ...
as with SED, awk handles files that can be redirected either by standard input, or when command-line arguments are passed in, and the edit order can be passed directly to the command-line arguments, or a script file can be specified with the-f parameter.

The format of the edit command is:

  /pattern/{actions}

Like SED, pattern is a regular expression, and actions are a series of actions. The AWK program reads the pending file one line at a time, if a line matches the pattern, or full condition condition,

Actions are performed, and if an awk command has only the Actions section, the Actions Act on each line of the file to be processed.

Example:

Note:

$: Indicates when to move forward

$: Represents the first column of the current row

$: Represents the second column of the current row

Shell's three large text processing tools grep, sed, and awk

Related Article

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.