3

I am ok with regular expressions in Perl but not had to do it in BASH before.

I tried to google for some sort of tutorial on it but didn't see any really good ones yet the way there are with Perl.

What I am trying to achieve is to strip /home/devtestdocs/devtestdocs-repo/ out of a variable called $filename and replace it with another variable called $testdocsdirurl

Hopefully that makes sense and if anybody has any good links that would be much appreciated.

Another way might be is if there is already a function someone has written to do a find and replace in bash.

1
  • I don't see the need for regular expressions here. Can you explain why you think you need them? Commented Jul 27, 2011 at 14:19

6 Answers 6

3

sed is the typical weapon of choice for string manipulation in Unix:

echo $filename | sed s/\\/home\\/devtestdocs\\/devtestdocs-repo\\//$testdocsdirurl/

Also, as hop suggests, you can use the @ syntax to avoid escaping the path:

echo $filename | sed s@/home/devtestdocs/devtestdocs-repo/@$testdocsdirurl@
Sign up to request clarification or add additional context in comments.

2 Comments

sed typically also understands s@pattern@string@ so you don't have to painfully escape slashes in a path.
@JG,@hop just to set the record straight - sed will actually take any character you like... so that even a silly expression like this will work echo SSSSS | sed 'sqSqsqg'
3

You can achieve this without a regular expression:

somepath="/foo/bar/baz"
newprefix="/alpha/beta/"
newpath="$newprefix${somepath##/foo/bar/}"

Comments

3

yes, bash supports regular expressions, e.g.

$ [[ 'abc' =~ (.)(.)(.) ]]
$ echo ${BASH_REMATCH[1]}
a
$ echo ${BASH_REMATCH[2]}
b

but you might rather want to use basename utility

$ f='/some/path/file.ext'
$ echo "/new/path/$(basename $f)"
/new/path/file.ext

excellent source of info is bash manual page

Comments

1

With bash

pattern=/home/devtestdocs/devtestdocs-repo/
testdocsdirurl=/tmp/
filename=/foo/bar/home/devtestdocs/devtestdocs-repo/filename
echo ${filename/$pattern/$testdocsdirurl}  # => /foo/bar/tmp/filename

Comments

0

Why do you need regular expressions for this?

These are just a few possibilities:

$ filename=/home/devtestdocs/devtestdocs-repo/foo.txt
$ echo ${filename/'/home/devtestdocs/devtestdocs-repo/'/'blah/'}
blah/foo.txt
$ basename $filename
foo.txt
$ realfilename=$(basename "$filename")

3 Comments

thanks, I just thought that using a regular expression would be the way to go on this one.
@martincarlin87: a good rule of thumb is: regex should be the last way you should go about solving any problem.
0

you're looking for an example of how use regular expressions in powershell?

is there an example here:

$input = "hello,123"
$pattern = ([regex]"[0-9]+")
$match = $pattern.match($input)
$ok = $input -match $pattern #return an boolean value if matched..
if($ok) { 
    $output = $match.groups[0].value
    [console]::write($output)
 } else {
   //no match
 }

in 'bash classic' regular expressions usage is precarious. you can use this: http://www.robvanderwoude.com/findstr.php

1 Comment

He asked for the UNIX shell bash (Bourne Again SHell), not for batch files or the Window PowerShell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.