Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is an sed expression that was created by a contributor here. More specifically by @craig-sanders.

$FileToMove is set to "2012-09-01 Home insurance quote_zz20.pdf"

# use sed to extract folder number from filename.
FolderNo=$(echo "$FileToMove" | sed -r -e 's/.*zz([0-9]+)\.pdf/\1/')
# On my Mac, I figured that this command needs to be FolderNo=$(echo "$FileToMove" | sed -E -e 's/.*zz([0-9]+)\.pdf/\1/')

The above command is used to extract "20" from the name of the file. Now I want to replace zz20 with null.

Been trying to do it. Will learn sed and before that need to learn regular expressions. In the meantime, I am sure it is super easy to replace it with null. Any tips?

share|improve this question

I presume you want to rename the file as well as move it.

Using my original script as a base, change this part of the script:

if [ "$NumFolders" -eq 1 ] ; then 
  mv "$ParentFolder/$FileToMove" "$TargetFolder/" 

to something like this:

if [ "$NumFolders" -eq 1 ] ; then
  NewFileName=$(echo "$FileToMove" | sed -E -e 's/_zz[0-9]+\././')
  mv "$ParentFolder/$FileToMove" "$TargetFolder/$NewFileName" 

Translating the regex to verbose English, the sed script here replaces "the first occurrence in the input line of '_zz followed by one-or-more digits followed by a literal full-stop'" with just a full-stop.

i.e. like Ben's answer it won't do the right thing with a file that has two or more '_zz20...' sequences in it. Easy enough to replace it with Birei's regexp (except get rid of the word 'null' between \1 and \2, that'll replace it with the literal string 'null'):

NewFileName=$(echo "$FileToMove" | sed -E -e 's/^(.*)zz[0-9]+(\.pdf)$/\1\2/')

BTW, this could have been done with one line and without the temporary variable ($NewFileName), but it's easier to understand this way.

share|improve this answer
btw, if you liked my original answer and it worked for you, then please Accept it - there should be a tick under the up and down vote buttons. – cas Sep 3 '12 at 14:04
Thank you Craig. I will add it into the script. Just did not get the alert that there was a response to this question. Need to figure out alerts on this site. – jim70 Sep 7 '12 at 14:11

If I understand your question, you don't want to extract anything, only to substitute the literal string zz20 with null. One way:

FolderNo=$(echo "$FileToMove" | sed -r -e 's/^(.*)zz[0-9]+(\.pdf)$/\1null\2/')

It saves in group 1 all characters until the zz20 and in group 2 all characters after the match. The only characters not saved will be zz20 that will be lost and substituted with null.

share|improve this answer

If you mean to replace with the concept of null, rather than the text "null", you could use this:

$ FolderNo=$(sed 's/_zz20//' <<< "2012-09-01 Home insurance quote_zz20.pdf")
$ echo $FolderNo
2012-09-01 Home insurance quote.pdf

This will replace the first occurence of _zz20 with nothing. This is because the second part of the argument to sed is empty (s/search/replace/ => s/_zz20//).

My example is not as careful as the one you cite, and the one @Birei suggests. If any of your files have multiple '_zz20' strings, things will not turn out well.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.