Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use regular expressions in Bash script

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use regular expressions in Bash scripts. It has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

Regular expressions (abbreviated as regex or regexp) are basically strings that define a search pattern, can be used to perform "search" or "search-and-replace" operations, and can also be used to verify conditions such as password policies.

Regular expressions are a very powerful tool at our disposal, and the advantage of using regular expressions is that they can be used in almost any computer language. So if you use Bash scripts or create a Python program, we can use regular expressions or we can write a single-line search query.

In this tutorial, we will learn some basic concepts of regular expressions and how to use them in Bash via grep, but if you want to use them in other languages like Python or C, you can only use the regular expressions part. So let's start with an example of a regular expression,

The regular expression looks like/t[aiou]l/.

But what does that mean? It means that the regular expression in question will look for a word that starts with t, contains any of the letters a e i o u in the middle, and the letter l is the most *** character. It can be tel, tal or til, and it can match a single word or part of another word like tilt, brutal or telephone.

grep The syntax for using regular expressions is $ grep "regex_search_term" file_location

If you don't understand, don't worry, this is just an example of what you can get with regular expressions, and believe me, it's the simplest example. We can get more from regular expressions. Now we'll start with the basics of regular expressions.

Recommended reading: useful linux commands you should know

Basic Regular Expressions

Now we begin to learn about some special characters called MetaCharacters MetaCharacters. They can help us create more complex regular expression search terms. Mentioned below is a list of primitive metacharacters,

. Points will match any character

[ ] will match a character range

[^ ] will match all characters except the one mentioned in parentheses

* Zero or more previous items will be matched

+ Matches one or more previous items

? Zero or one preceding item will be matched

{n}will match the previous term n times

{n,} will match n or more previous items

{n,m} will match items between n and m times

{,m} will match items less than or equal to m times

\is an escape character that we use when we need to include a metacharacter in our search

We will now discuss all of these metacharacters with examples.

. (dot)

It is used to match any character that appears in our search term. For example, we can use points such as:

$ grep "d.g" file1

This regular expression means that we are looking for a string of words in a file named 'file1' that starts with d, ends with g, and can have one character in between. Similarly, we can use any number of points as our search pattern, such as T... h, this query term will look for a word that starts with T and ends with h, and can have any six characters in between.

[ ]

Square brackets are used to define character ranges. For example, we need to search for some particular word instead of matching any character,

$ grep "N[oen]n" file2

Here, we are looking for a word that starts with N, ends with n, and has only one of o, e, or n in between. In square brackets we can refer to a single to any number of characters.

We can also define something like a-e or 1-18 in square brackets as a list of matching characters.

[^ ]

This is like the not operation of a regular expression. When [^ ] is used, it means that our search will include all characters except those mentioned in square brackets. For example,

$ grep "St[^1-9]d" file3

This means that we can have all such words that start with St, end with the letter d, and must not contain any number from 1 to 9.

So far, we've only used examples of regular expressions that only need to find a single character in the middle, but what if we need more characters? Suppose we need to find all words that start and end with one character, and there can be any number of characters in between. This is where we use multiplier meta characters like + * and? in the United States.

{n},{n,m},{n,}, or {,m} are also other multiplier characters that can be used in our regular expression terms.

* (asterisk)

The following example matches any number of occurrences of the letter k, including none:

$ grep "lak*" file4

It means we can match lake, la or lakk.

+

The following pattern requires that the letter k in the string be matched at least once:

$ grep "lak+" file5

Here k needs to occur at least once in our search, so our result can be lake or lakk, but not la.

?

In the following pattern matches

$ grep "ba? b" file6

Match the string bb or bab, using? Multiplier, we can have one or zero character occurrences.

A very important tip.

This is very important when using multipliers, assuming we have a regular expression

$ grep "S.* l" file7

Shane is a little to play ball. Shane is a little to play ball? We were just looking for words in the search, why we got the whole sentence as our output.

This is because it meets our search criteria, it starts with the letter s, has any number of characters in the middle and ends with the letter l. So, what can we do to correct our regular expressions to just get words instead of whole sentences as our output?

Do we need to add to our regular expressions? Metacharacters,

$ grep "S.*? l" file7

This will correct the behavior of our regular expressions.

\

\is used when we need to include a metacharacter or character with special meaning to a regular expression. For example, we need to find all words ending in dots, so we can use:

$ grep "S.*\\. " file8

This will find and match all words that end with a dot character.

Thank you for reading this article carefully. I hope that Xiaobian's "How to use regular expressions in Bash scripts" article will help everyone. At the same time, I hope everyone will support you more. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report