Shell script learning: quick understanding of Regular Expressions in grep

Source: Internet
Author: User

Shell scripts are the core of Linux, while regular expressions are the core of shell scripts. Understanding regular expressions can quickly match the content to be searched, lay a solid foundation for future shell script programming.

 

The contact regular expression generally starts with the grep command, for example:

Grep "^ # [: Space:] *"/etc/inittab

In this command, "^ # [: Space:] *" is the content of the regular expression, which means "starting with # And followed by any space", combined with the grep command, is to display the content of "starting with # followed by any space" in the/etc/inittab file.

The grep command is used to search and print the matched content.

Grep [Option] file to be copied using the regular expression

-I is case insensitive.

-Color to highlight matched text content

-E: Use extended metacharacters.

-V Inversion

So what are the regular expressions?

-----------------------------------------------------

In Shell, regular expressions are divided into standard metacharacters and extended metacharacters.

Standard metacharacters: ^ $. * [] [x-y] [^] \ <\> \ (...) \ x \ {M \ n}

Extended metacharacters: ^ $. * [] [^] +? A | B ()

Related materials: http://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F

# You can find their meanings in the above link.

Another important knowledge: POSIX square brackets Character Set

[: Alnum:] [: lower:] [: xdigit:]

[: Alpha:] [: Print:] [: blank:]

[: Blank:] [: punct:]

[: Cntrl:] [: Space:]

[: Graph:] [: Upper:]

In the above example grep "^ # [: Space:] *"/etc/inittab, [: Space:] indicates a blank character, the combination of POSIX character sets and regular expressions can produce powerful functions. In many cases, we can use it for our own purposes.

For example, display the lines starting with one or more blank characters in the/boot/GRUB/grub. conf file

grep "^[[:space:]]\{1,\}" /boot/grub/grub.conf

# Note the use of ^ and [] In this command. Generally, ^ [] is used to match a character starting with ^ [: Space:]. because it must start with a blank character, use [[: Space:] instead of [: Space:]. If the error is written as ^ [: Space:] now we know the function of []: to pin a character. Multiple characters indicate multiple possibilities.

 

 

Below are a few more difficult exercises to learn and understand regular expressions.

1. display the rows starting with a number in the/etc/inittab file and ending with the same number as the starting number;
2. The ifconfig command can display information related to the IP address of the current host. If you use the grep or other text processing commands to retrieve the IP addresses of the local host, you must not package 127.0.0.1;
3. Display/etc/sysconfig/network-scripts/The ifcfg-eth0 file contains a line similar to an IP address point in decimal format;

 

 

# Answer:

1. grep "^ \ ([0-9] \). * \ 1 $"/etc/inittab

2. ifcofig | grep "Inet ADDR" | grep-V '2017. 0.0.1 '| cut-D:-F2 | cut-d "" F1

3. grep-e "([0-9] {1, 3 }\.) {3 }\. [0-9] {1, 3} "/etc/sysconfig/network-scripts/ifcfg-eth0

Grep "[0-9] \ {1, 3 \}\. \ "{3 \}\. [0-9] \ {1, 3 \}/etc/sysconfig/network-scripts/ifcfg-eth0

# Resolution

1. ^ [0-9] Use \ (... \) \ 1 to send ^ [0-9] To \ 1 $ to pin the end

2. grep first proposes to remove content containing Inet ADDR.-V extracts '2017. 0.0.1 'and uses the cut command-D to intercept the first field.

3. use-e solution ([0-9]) \ {1, 3} [0-9] numbers appear at least once, at most 3 \. escape. the symbol {3} shows the content of the previous () three times \. [0-9] {} matches the following three digits

The grep solution can be figured out by yourself.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.