Sed and awk usage

Source: Internet
Author: User

Syntax for regular Expressions:

. A dot represents a character. Represents any character. Example 1: Style. T., which represents 3 characters, the middle is T, and the left and right sides are any one character. Example 2: ... A string representing a character length of 3. If you want to compare. The character itself, plus the escape character (\), is written as \. Example: Style data\ .... Represents data. followed by 3 characters, such as Data.txt, Data.cfg, and data. 123 and so on, all conform to this style, but data1234 does not conform, because 4 points the leftmost point, has been used to escape its special meaning, revert to. This character itself, therefore, \ .... and 1234 than the match. 
^ at the beginning of the line ^123  this style, but Hi Jack does not match, because Jack does not appear at the front of the row. 
$ at the tail  -  the 123 conforms to this style.
[...] character set [...] represents one character in a string line (1 characters long). Example 1: Style [ABc], which represents one of the 3 characters of a or B or C. Example 2:[ss]ame represents same or same. The following are common usages: [a-z] An uppercase letter [a-z] A lowercase letter[0-9] A number [^a-Z] A character other than the capital letter [^a-za-Z] A non-English letter character [^a-za-z0-9] a non-English letter, non-numeric character
^ The first position that appears in parentheses represents the meaning of "non/Not".
* appears more than 0 * represents the preceding (left-hand) character has 0 or more than 0. For example: The style AA*C, which represents a this character may appear 0 or more than 0. For example: AC, AAc, AAAC, AAAAC all conform to this style. 
\{...\} Specifies the number of preceding (left-hand) characters specified by the number of matches. For example: \{3,5\} indicates that the preceding characters have 3~5 . [a-z]\{3,5\} represents a string of lowercase letters with a length of 3~5.
\ (... \) temporarily save the matching string for example: h\ (... \) y indicates that you want to save 3 characters between H and Y. To extract a saved string, the available positional arguments, \1 , represent the first saved string, \2 for the second, and so on. 

The use of SED

The syntax for SED is as follows:

' Style Commands ' file

It means that if a line in the file conforms to the style, it executes the specified SED command, such as delete (d) or replace (s). The "style" here uses a pair//enclosed to indicate the meaning of the search; You can also specify the range of data rows, for example: 1,6 indicates that the range is from line 1th to line 6th, and/aaa/,/ddd/indicates that the scope is from the data row containing AAA to the data row containing the DDD.

Please pay special attention: SED does not change the file content. SED works by reading the contents of the file, and after the stream is edited, the results are displayed to the standard output. Therefore, if you want to store the processing results of the SED, you have to use the steering output to save the results to another file.

Various uses of SED:

' 1,4d ' DATAF1 the 1th to the 4th row of data, the rest is displayed. D is the delete command for SED. 
How to use sed 2: Delete data rows that contain "styles". Sed'/la/d'DATAF3 Delete The row containing LA, and the rest is displayed. Among them,/ /represents the meaning of the search. Sed'/[0-9]\{3\}/d'dataf3 contains "3The number of digits "of the line is deleted, and the rest is displayed. In the style [0-9]\{3\}, \{3\} Table//the string to look for is a 3-digit number. Sed'/^$/d'dataf5 Delete the blank line of the DATAF5. ^At the beginning of the table, the tail of the $ table, which has no characters between them, represents the line as a blank line. 
' /la/!d ' dataf3 Delete rows that do not contain LA, and the rest is displayed. Here is the meaning of the negative, indicating that does not conform to the style of the person.
' /la/p ' the '/la/p'  dataf3 option -n suppresses sed from displaying the default action for other rows, displaying only rows of data that match the style.
the use of SED 5: Replace. Sed-N's/la/oo/p'DATAF3 Here S is to replace the meaning of the first pair//The string enclosed in (LA) is the target of the search, and the second pair//containingincludes the permutation string (Oo). It will change the string in the data row to OO. Please note: The above command will only replace the first occurrence of LA, if you want to complete the replacement, you should add the global command G, as follows: SED-N'S/LA/OO/GP'DATAF3 This will change all the found LA to OO. The use of substitution, there are also the following: Sed-N's/la//p'DATAF3 Deletes the first occurrence of LA in each row (the substitution of La into an empty string is deleted). Sed's/^...//'DATAF3 Remove the 3 characters from the beginning of each line. Sed's/...$//'DATAF3 removes 3 characters from the end of each line. 
 -n  " s/\ (la\)/\1oo/p   '  
' /aaa/s/234/567/p '  '/aaa/,/ddd/s/b/567/p'2,4s/b/567/p'  dataf3 from line 2nd to line 4th, the B is replaced by 567. The above explanation shows that the power of sed dynamic editing is quite powerful, and it complements Bash's ability to modify files. 

Sed and awk usage

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.