Regular expression and file formatting

Regular expression and file format processing

1. What is regular expression

Regular expression is the way to deal with characters. It deals with strings in behavioral units. For administrators, regular expression is a good thing to learn. It is important to remember that there is no relationship between regular expressions and wildcards.

2. Some advanced parameters of grep

2.1. Basic regular expression exercise

First, download the data on brother bird's website.

zhangsan@Aliyun:~$ wget http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt
zhangsan@Aliyun:~$ vim regular_express.txt
  1 "Open Source" is a good mechanism to develop programs.
  2 apple is my favorite food.
  3 Football game is not use feet only.
  4 this dress doesn't fit me.
  5 However, this dress is about $ 3183 dollars.^M
  6 GNU is free air not free beer.^M
  7 Her hair is very beauty.^M
  8 I can't finish the test.^M
  9 Oh! The soup taste good.^M
 10 motorcycle is cheap than car.
 11 This window is clear.
 12 the symbol '*' is represented as start.
 13 Oh!     My god!
 14 The gd software is a library for drafting programs.^M
 15 You are the best is mean you are the no. 1.
 16 The world <Happy> is the same with "glad".
 17 I like dog.
 18 google is the best tools for search keyword.
 19 goooooogle yes!
 20 go! go! Let's go.
 21 # I am VBird
 22 

The above content is the content of bird brother connection.
The first step is to find a specific string, as shown below.

zhangsan@Aliyun:~$ grep -n 'the' regular_express.txt 
8:I can't finish the test.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.

Second, we use brackets to find

zhangsan@Aliyun:~$ grep -n 't[ea]st' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.

Through this example, we can see that the role of [] is to replace [e a] to indicate that both E and a can be used.
Beginning ^ and end of line$
In addition, we can also remove some special characters through the first ^ and last $

# ^It's a term that starts with tom
zhangsan@Aliyun:~$ grep '^tom' passwd 
tom sys:x:3:3:sys:/dev:/usr/sbin/nologin
# $stands for a word ending in tom
zhangsan@Aliyun:~$ grep 'tom$' passwd 
games:x:5:60:games:/usr/games:/usr/sbin/nologintom

If I want to find out which line is blank, my operation is:

zhangsan@Aliyun:~$ grep -n '^$' regular_express.txt
22:

Arbitrary characters. And repeating characters*

zhangsan@Aliyun:~$ grep -n 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".

Restrict consecutive RE characters {}

# Find characters with four o's between tm
zhangsan@Aliyun:~$ grep 'to\{4\}m' passwd 
toooom sys:x:3:3:sys:/dev:/usr/sbin/nologin
# Find 4-9 o characters between tm
zhangsan@Aliyun:~$ grep 'to\{4,9\}m' passwd 
toooom sys:x:3:3:sys:/dev:/usr/sbin/nologin
tooooooooomsync:x:4:65534:sync:/bin:/bin/sync
tooooomman:x:6:12:man:/var/cache/man:/usr/sbin/nologin

3.sed tools

The sed command can find and modify the characters that meet the requirements.

zhangsan@Aliyun:~$ sed '1,2d' hosts
# Delete the first and second lines of the hosts file
# sed put the file in memory, and then modify it, only in memory
zhangsan@Aliyun:~$ sed -i '1,2d' hosts
# Modify directly to file

4.awk's easy-to-use data processing tools

The awk command can help us find the content of interest and then typeset. Its operation format is: awk -F 'condition {operation}' file

zhangsan@Aliyun:~$ ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 162.17.172.171  netmask 255.255.240.0  broadcast 162.16.165.255
        ether 00:16:3e:10:b4:c7  txqueuelen 1000  (Ethernet)
        RX packets 41449753  bytes 9972007192 (9.9 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 41068356  bytes 6352574318 (6.3 GB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
# Grab 162.17.172.171
zhangsan@Aliyun:~$ ifconfig eth0 | grep 'inet' | awk -F" " '{print $2}'
162.17.172.171
Published 9 original articles, won praise 2, visited 131
Private letter follow

Tags: Google Linux vim

Posted on Fri, 14 Feb 2020 08:27:20 -0500 by scavok