Bash:
Using regular expressions
How to:
Basic Pattern Matching
To find if a string matches a pattern, you can use grep
, a command-line utility for searching plain-text data sets for lines that match a regular expression:
echo "Hello, World!" | grep -o "World"
# Output: World
Extracting Specific Data
To extract parts of data that match your regex patterns, you can use -o
with grep
:
echo "Error: File not found" | grep -oE "[A-Za-z]+:"
# Output: Error:
Using Regex with sed
sed
(stream editor) is a powerful utility for parsing and transforming text. Here’s how to use sed
with regex to replace text:
echo "Bash is great" | sed -e 's/great/awesome/'
# Output: Bash is awesome
Pattern Matching in Conditional Statements
Bash also supports regex in conditional statements directly:
[[ "https://example.com" =~ ^https?:// ]] && echo "URL is valid" || echo "URL is invalid"
# Output: URL is valid
Advanced Pattern Matching and Manipulation with awk
awk
is another text-processing tool that supports more complex data extraction and manipulation. It can be beneficial when working with structured text data, like CSVs:
echo -e "ID,Name,Age\n1,John,22\n2,Jane,24" | awk -F, '$3 > 22 {print $2 " is older than 22."}'
# Output: Jane is older than 22.
While Bash’s built-in regex functionalities cover many use cases, for very advanced regex operations, you might consider using a combination of Bash scripts with perl
or python
scripts, as these languages offer powerful regex libraries (e.g., re
in Python). A simple example with Python:
echo "Capture this 123" | python3 -c "import sys; import re; print(re.search('(\d+)', sys.stdin.read()).group(0))"
# Output: 123
Incorporating these programming languages when necessary can help you leverage the full power of regex in your Bash scripts.