Shell scripting Knowledge (c) Regular expressions grep sed awk

Source: Internet
Author: User

1. Regular expressions


Matches all words in the given text: (? [a-za-z]+?] to match the spaces that may appear before and after a word.

Match IP address: [0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3} 4 integers separated by parentheses.

2, grep "Mach_text" File1 file2 file3 ....

Recursive search file: grep "text"./-rn

3, cut the text in the column into the segmentation. You can specify delimiters for each column that is split. In cut terms, one field per column.

cut–f 2,3 filename Displays columns 2nd and 3rd.

Print out all columns except column 3rd: cut–f3–complement filename

3. Sed

Stream editor. You can replace a string in the given text.

Sed ' s/pattern/replace_string/' file uses –i to apply the replacement result to the source file. This command simply replaces the first conforming content in each row, and if you want to replace everything, you need to add g at the end of the command:

Sed ' s/pattern/replace_string/g ' file

Remove blank line: sed '/^$/d ' file

Matched string tag &: Echo Thisis an example | Sed ' s/\w\+/[&]g '

[This] [IS] [An] [Example]

The regular expression \w\+ matches each word, and then replaces it with [&]. & corresponds to a previously matched word.

4. awk

You can manipulate columns and rows.

awk ' begin{commands} pattern {commands} end{commands} ' filename

An awk script typically consists of 3 parts: A BEGIN statement block, an end statement block, and a common block of statements that can use pattern matching. The 3 sections are optional.

(1) Executes the statement in the Begin{commands} statement block.

(2) Read a line from the file or stdin and execute pattern{commands}. Repeat the process until all the files have been read.

(3) Executes the end{commands} statement block when reading to the end of the input stream (InputStream).

Echo–e "Line1\nline2" | awk ' begin{print ' Start '} {print} end{print "END"} '

Special variables:

NR: Represents the Record count (number of records), which corresponds to the current line number during execution.

NF: Indicates the number of fields in the field, which corresponds to the number of fields in the current period during execution.

$: This variable contains the text content of the current line during execution.

$: Contains the text content of the first field.

awk ' {print $} ' filename prints the fifth column.

Print permissions and file names for files in the current directory:

awk ' {print $ ': ' $9} '
Total
-rw-rw-r--.: Bsdmakefile
Drwxrwxr-x.:config
-rw-rw-r--.: config.in
Drwxrwxr-x.:d OCS
Drwxrwxr-x.:feeds
-rw-rw-r--.: Feeds.conf.default


To remove a sentence that contains a word in a file:

Sed ' s/[^.] *mobile phone[^.] *\ . G ' filename

Remove the sentence that contains the word "mobile phone".

The format of the expression is: ' s/match sample/substitution string/g ' [^.] * You can match any combination of characters except the period. Each matched sentence is replaced by//(no content between/and/)


Shell scripting Knowledge (iii) regular expression grep sed awk

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.