Grep command, grep command

Source: Internet
Author: User

Grep command, grep command

Grep is one of the most widely used commands in UNIX and LINUX. Grep allows mode searches for text files. If the matching mode is found, grep prints all rows containing the mode. Grep supports basic regular expressions and its extension set. Grep has three types of Deformation:

Grep: Standard grep command. This format is mainly discussed here;

Egrep: equivalent to grep-E. It extends grep and supports basic and extended regular expressions;

Fgrep: equivalent to grep-F. It does not support regular expressions and allows searching strings rather than a pattern.

Grep has many options for use. If you have any questions, please refer to "man" for details. Here I am just talking about what I have used. Recently I need to write some shell scripts, the grep command is used in this process. The following are several application scenarios:

1. Check whether the specified content in all files under directory A exists in the corresponding file under directory B.

Solution: Use grep to output the specific content of each file in directory A to another file, and use the following command to process directory B:

Grep "define" fileA> listA

Grep "define" fileA> listB

Because the content after define is different, our goal is to find a format similar to define ("TEST_A", "testa"); while the file does not have TEST_A, of course, there are many different define statements. If the diff command is used for comparison, it is difficult to cope with this problem. Therefore, you can use the BeyondCompare tool in windows for manual comparison. Different contents are marked with red at a glance, at least the same part before the comma will not be highlighted in red. You can search for this tool if you are interested.

2. Package and compress all content except individual files in a directory.

Solution: Use grep-v. Note that it is lower-case v, meaning that it does not contain matched rows, and upper-case V indicates version.

The complete command can be written as follows:

Ls PATH | grep-v '. log' | grep-v '.tar.gz'> tar. list

To write the directory name (excluding .log..tar.gz extension files) of the compressed file to tar. list

Step 2: Perform the packaging operation:

Cat tar. list | xargs tar-zcvf target.tar.gz

Pass the cat output result as a parameter to the pipeline and run the packaging command. This is also the xargs usage.

Http://hovertree.com/menu/linux/

3. Find the latest directory that meets the "target _ number" format under the directory.

Solution: Use grep-e. Of course, use-E.

The complete command is:

Ls PATH -- sort = time | grep-e resource _ \ d * | head-1

Use the ls option -- sort = time to sort the time, and the head is the first part of the hit.

Recommended: http://www.cnblogs.com/roucheng/p/3470287.html

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.