Home > Software design >  Trouble translating this regex to bash
Trouble translating this regex to bash

Time:05-18

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!

CodePudding user response:

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"
}

CodePudding user response:

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:

  • the function keyword is optional
  • there can be whitespace between the function name and the parentheses
  • the opening brace does not have to appear on same line

CodePudding user response:

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

CodePudding user response:

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{"
  • Related