0
0
Bash Scriptingscripting~15 mins

Quantifiers (*, +, ?) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Quantifiers (*, +, ?) in Bash Scripting
📖 Scenario: You are working on a bash script that processes a list of filenames. Some filenames have different patterns, and you want to filter them using regular expressions with quantifiers.Quantifiers like *, +, and ? help match patterns that repeat or appear optionally.
🎯 Goal: Build a bash script that uses grep with regular expressions containing quantifiers *, +, and ? to filter filenames based on specific patterns.
📋 What You'll Learn
Create a variable files containing a list of filenames separated by spaces.
Create a variable pattern_star with a regex using the * quantifier.
Use grep -E with pattern_star to filter filenames and save to result_star.
Print the filtered filenames stored in result_star.
💡 Why This Matters
🌍 Real World
Filtering and processing filenames or text data using regex patterns with quantifiers is common in scripting tasks like log analysis, file management, and automation.
💼 Career
Understanding regex quantifiers and using them in bash scripts is a valuable skill for system administrators, DevOps engineers, and automation specialists.
Progress0 / 4 steps
1
Create a list of filenames
Create a variable called files and assign it the exact string: file1.txt file22.txt file333.txt fileA.txt fileB.txt
Bash Scripting
Need a hint?

Use double quotes to assign the list of filenames as a single string to files.

2
Create a regex pattern with the * quantifier
Create a variable called pattern_star and assign it the regex string file[0-9]*\.txt which uses the * quantifier to match zero or more digits after 'file'.
Bash Scripting
Need a hint?

Remember to escape the dot . with a backslash \ in regex.

3
Filter filenames using grep with the * quantifier pattern
Use grep -E with the variable pattern_star to filter the filenames in files. Save the filtered filenames into a variable called result_star. Use echo and command substitution.
Bash Scripting
Need a hint?

Use grep -E -o to output only matching parts. Use $( ) for command substitution.

4
Print the filtered filenames
Print the variable result_star to display the filtered filenames.
Bash Scripting
Need a hint?

Use echo "$result_star" to print the filtered filenames, each on a new line.