Reputation: 803
I have the following working regex that I tested in regex101
^function\s(\w+)\(\)\s{$
for the following test string
function hello() {
echo "Hello World"
}
function get_aws_credentials() {
echo "Getting AWS Credentials
}
My goal is to get all function names defined in that file and that's what my original regex does, but not in bash.
The problem is that it doesn't work in bash or zsh (I really only care about bash though).
I have researched it and saw some alternatives, like replacing the \w+
for [[:alnum:]]
, but that didn't work either.
This is how I'm testing it: cat utils.sh | grep "^function\s(\w+)\(\)\s{$"
Any idea of what I'm missing here?
Thanks!
Upvotes: 1
Views: 60
Reputation: 490
The problem is that '\w' is not a valid character class in bash regular expressions.
You can use the '[[:alnum:]]' character class instead:
cat utils.sh | grep "^function\s\([[:alnum:]]+\)\(\)\s{"
Alternatively, you could use the '[[:alpha:]]' character class, which would match any letter:
cat utils.sh | grep "^function\s\([[:alpha:]]+\)\(\)\s{"
Upvotes: 0
Reputation: 11237
Using grep
$ grep -Po '^function \K[^(]*' utils.sh
hello
get_aws_credentials
Using sed
$ sed -n s'/^function \([^(]*\).*/\1/p' utils.sh
hello
get_aws_credentials
Upvotes: 1
Reputation: 246932
Have to be careful with regex sites: you need to know what flavor of regex you're using.
Your grep might have a -P
option to enable PCRE, so try this:
grep -oP '^\s*(function\s+)?\K\w+(?=\s*\(\))'
Given your input, this outputs
hello
get_aws_credentials
A couple of notes about the pattern:
function
keyword is optionalUpvotes: 1
Reputation: 2120
Would this work or are you trying to create a universal regex for every file type?
# Get all function names defined in a bash script file
# Usage: script.sh <script_file>
function get_functions() {
local FUNC_NAME=$(basename $1)
local FUNC_NAMES=$(grep -o '^function [a-zA-Z0-9_]*' $1 | sed 's/function //')
echo "$FUNC_NAMES"
}
Upvotes: 1