Regular Expressions (RegEx) on Linux

Regexps are most commonly used with the Linux commands:- grep, sed, tr, vi.

The backtick(`) is actually called command substitution.

Some basic regular expressions:

Sr. no. Symbol Description
1. . It is called a wild card character, It matches any one character other than the new line.
2. ^ It matches the start of the string.
3. $ It matches the end of the string.
4. * It matches up to zero or more occurrences i.e. any number of times of the character of the string.
5. \ It is used for escape following character.
6. () It is used to match or search for a set of regular expressions.
7. ? It matches exactly one character in the string or stream.

Text File Used: Fruits-name, it is a text file with MS-DOS line terminator which need to be coverted to Unix text file. Or create a file with the name of fruit.txt, and filled with the content below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Apple
Apricot
Avocado
Banana
Bilberry
Blackberry
Blackcurrant
Blueberry
Boysenberry
Currant
Cherry
Cherimoya
Chico fruit
Cloudberry
Coconut
Cranberry
Cucumber
Custard apple
Damson
Date
Dragonfruit
Durian
Elderberry
Feijoa
Fig
Goji berry
Gooseberry
Grape
Raisin
Grapefruit
Guava
Honeyberry
Huckleberry
Jabuticaba
Jackfruit
Jambul
Jujube
Juniper berry
Kiwano
Kiwifruit
Kumquat
Lemon
Lime
Loquat
Longan
Lychee
Mango
Mangosteen
Marionberry
Melon
Cantaloupe
Honeydew
Watermelon
Miracle fruit
Mulberry
Nectarine
Nance
Olive
Orange
Blood orange
Clementine
Mandarine
Tangerine
Papaya
Passionfruit
Peach
Pear
Persimmon
Physalis
Plantain
Plum
Prune
Pineapple
Plumcot
Pomegranate
Pomelo
Purple mangosteen
Quince
Raspberry
Salmonberry
Rambutan
Redcurrant
Salal berry
Salak
Satsuma
Soursop
Star fruit
Solanum quitoense
Strawberry
Tamarillo
Tamarind
Ugli fruit
Yuzu

1.0. Using “.” (dot) to match strings.

Script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/sh

# Basic Regular Expression

# 1. Using “.” to match strings.

# loading the text file

fruits_file=`cat fruit.txt | grep App.e`

# here the original (answer) word will be Apple,

# but because we don’t know the spelling of the Apple,

# we will put a dot (.) in that place.

echo "1. Using '.' to find the original word, whereas given word is 'App.e'"

# displaying output

echo "Output:"

echo "$fruits_file"

Output:

1
2
3
4
$ sh regex.dot.sh 
1. Using '.' to find the original word, whereas given word is 'App.e'
Output:
Apple

image-20220405194409111

2.0. Using “^” (caret) to match the beginning of the string

1
2
3
4
5
6
7
8
$ cat fruit.txt | grep ^B
Banana
Bilberry
Blackberry
Blackcurrant
Blueberry
Boysenberry
Blood orange

image-20220405194437845

3.0. Using “$” (dollar sign) to match the ending of the string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ cat fruit.txt | grep e$
Apple
Custard apple
Date
Grape
Jujube
Lime
Lychee
Cantaloupe
Nectarine
Nance
Olive
Orange
Blood orange
Clementine
Mandarine
Tangerine
Prune
Pineapple
Pomegranate
Quince
Solanum quitoense

image-20220405194515192

4.0. Using “*” (an asterisk) to find any number of repetitions of a string

1
2
3
$ cat fruit.txt | grep ap*le
Custard apple
Pineapple

image-20220405194547593

5,0. Using “\” (a backslash) to match the special symbol

1
2
3
4
5
6
7
8
9
10
11
12
$ cat fruit.txt | grep "\ "
Chico fruit
Custard apple
Goji berry
Juniper berry
Miracle fruit
Blood orange
Purple mangosteen
Salal berry
Star fruit
Solanum quitoense
Ugli fruit

image-20220405194608766

6.0. .Using “()” (braces) to match the group of regexp.

1
2
3
4
5
6
7
8
9
10
$ cat fruit.txt | grep -E "(fruit)"
Chico fruit
Dragonfruit
Grapefruit
Jackfruit
Kiwifruit
Miracle fruit
Passionfruit
Star fruit
Ugli fruit

7.0. Using “?”(question mark) to find all the matching characters

1
2
3
4
5
6
7
8
9
10
11
12
$ cat fruit.txt | grep -E Ch?
Currant
Cherry
Cherimoya
Chico fruit
Cloudberry
Coconut
Cranberry
Cucumber
Custard apple
Cantaloupe
Clementine

image-20220405194727369


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!