0

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?

jim70
  • 173

4 Answers4

2

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/" 
else

to something like this:

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

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.

cas
  • 78,579
  • 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 03 '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 07 '12 at 14:11
1

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.

Birei
  • 8,124
1

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.

0

Using Perl's rename:

rename -n 's/.*zz([0-9]+)\.pdf/$1/' ./*

Remove -n (aka dry-run) when the output looks satisfactory.