Search code examples
linuxbashsubstring

Bash substring with regular expression


In a bash script, I´d like to extract a variable string from a given string. I mean, i´d like to extract the string file.txt from the string:

This is the file.txt from my folder.

I tried:

var=$(echo "This is the file.txt from my folder.")
var=echo ${var##'This'}
...

but I´d like to make it in a cleaner way, using the expr, sed or awk commands.

Thanks

Edited:

I found another way (nevertheless, the answer with the sed command is the best one for me):

var=$(echo 'This is the file.txt from my folder.')
front=$(echo 'This is the ')
back=$(echo ' from my folder.')
var=${var##$front}
var=${var%$back} 
echo $var

Solution

  • The following solution uses sed with s/ (substitution) to remove the leading and trailing parts:

    echo "This is the file.txt from my folder." | sed "s/^This is the \(.*\) from my folder.$/\1/"
    

    Output:

    file.txt
    

    The \( and \) enclose the part which we want to keep. This is called a group. Because it's the first (and only) group which we use in this expression, it's group 1. We later reference this group inside of the replacement string with \1.

    The ^ and $ signs make sure that the complete string is matched. This is only necessary for the special case that the filename contains either "from my folder." or "This is the".