Application of shell script learning advanced and regular expression

Source: Internet
Author: User
Tags first string egrep

Sort order:

-F:     ignore character case-N:    Compare numeric Size-T:    specify delimiter    -K: Specify to compare field ordinals after delimited-u:    duplicate rows, only once

 

Sort by the second row of values:

$ sort-k2n SORT.TXTCCC 8aaa 11yyy 12eee 22

Duplicate rows are displayed once: Sort-u sort.txt

Uniq handles duplicate rows , using the sort together:

WC command:

Line | Characters | Word $ WC sort.txt 6 sort.txt$ wc-l sort.txt 6 sort.txt$ wc-c sort.txt (sort.txt$ wc-w sort.txt) sort.txt

  

Find command:

Find by name $ find./-name "*.sh" Find the normal file under the current level directory $ find/-maxdepth 1-type F Find the size between 1M and 5M find to-size +1m-size-5m look for the log directory 10 days Previously created file $ find ~/log-ctime +10 Find the log directory created within 5 days of the file $ find ~/log-ctime-5

  

The LS command cannot read a pipe or standard input and can use EXEC instead:

Find ~/log-ctime +10-exec Ls-la {} \;

  

Using the OK option is more secure and requires confirmation:

Find ~/log-ctime +10-ok rm-f {} \;

  

Of course, you can also use Xargs to parse the output of the previous command into small chunks:

Find./-maxdepth 1-type-f | Xargs ls-l

  

The grep command can be used to find content:

-R Recursive Sub-directory

-I ignores case

-V Exclusion

The regular expression shows:

Character class:

Quantity limit:

Position qualifier:

Special characters:

You can use the EGREP command to support an extended regular expression:

Example 1: Match out all IP addresses XXX.XXX.XXX.XXX

Egrep ' ^[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3}$ ' All.txt

Example 2: Matching mailbox [email protected]| CN

Egrep ' [0-9a-za-z][email protected][0-9a-za-z]+\. (COM|CN) $ ' all.txt

Note: There is currently a basic regular and extended regular, the foundation is not supported? +  { }  | ().

If you want to support, you need to escape the character, write to write the extended regular, write the basic regular.

grep ' [0-9a-za-z]\[email protected][0-9a-za-z]\+\.\ (com|cn\) $ ' all.txt or grep-e ' [0-9a-za-z][email protected][ 0-9a-za-z]+\. (COM|CN) $ ' all.txt

Finally, the matching IP address is optimized once:

  

sed tool , Stream Editor, and VI of the same origin, processing files, modify the text!

Common usage:

SED option ' script ' file1 file2 ...               ' Sed parameters    ' script (/pattern/action) ' Pending file sed option-f scriptfile file1 file2 ...           Sed parameter –f ' script file ' pending file

Parameter description:

A,    append             append i,    insert             insert D,    delete             delete s,    substitution     Replace

Example: inserting XXXX in the second line

Sed ' 2ixxxx ' cash.sh

-I is a direct modification of the source file and requires careful use

Delete 4–6 line:

Sed ' 4,6d ' cash.sh

Replace read for write:

Sed ' s/read/write ' cash.sh

Sed Common directives:

/pattern/p prints lines that match the pattern/pattern/d Delete rows matching the pattern/pattern/s/pattern1/pattern2/find rows that match the pattern, Replace the first string that matches pattern1 in the row with pattern2/pattern/s/pattern1/pattern2/g to find the line that matches the pattern, and replace all strings that match pattern1 in the row with pattern2

For example, match echo and print the line:

Sed '/echo/p ' cash.sh

  

Multiple lines are displayed, and if you need to not enter the source file content, you can use:

Sed '/echo/p ' cash.sh-n

  

Use of address symbols:

Sed ' s/23/-&-/' cash.sh

  

\1 \2 represents the corresponding first cell and second cell

-R can be used to extend the regular

Remove HTML Tags:

Sed-r ' s/<[(/|0-9a-za-z)]+>//g ' hello.html or sed-r ' s/<[^<>]+>//g ' hello.html

  

 

  

Application of shell script learning advanced and regular expression

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.