0

I have huge number of files with some names ahead of real filename. Following is the example of the problem:

Nora Hmann - sub-01.zip
Nora Hmann - sub-02.zip
Nora Hmann - sub-03.zip
Nora Hmann - sub-04.zip
Nora Hmann - sub-05.zip
Nora Hmann - sub-06.zip

Now using sed commnd i want to remove Nora Hmann - and just keep sub-01.zip. can some one help me with sed command as string to be replaces contains spaces, sed is not doing its properly.

  • 1
    Spaces are not an issue for sed. Please [edit] your question and show us the command you were using. Also, please clarify if all your lines will contain Nora Hmann -. Would it be enough to remove that specific string? Can we always remove everything before the first -? What will change on the different lines? – terdon Jan 10 '17 at 13:20
  • also, can you clarify you want to rename filenames or is this text processing of file contents? – Sundeep Jan 10 '17 at 13:43

5 Answers5

4

Erasing list of this names to suffix after the last space:

sed 's/.* //'

But if it is couple of files in directory, it's simpler to use rename:

rename 's/^.* //' *
ValeriyKr
  • 929
3

Use the following command:

sed -ie "s/Nora\ Hmann\ \-\ //g" stack

before:

Nora Hmann - sub-01.zip
Nora Hmann - sub-02.zip
Nora Hmann - sub-03.zip
Nora Hmann - sub-04.zip
Nora Hmann - sub-05.zip
Nora Hmann - sub-06.zip

after:

sub-01.zip
sub-02.zip
sub-03.zip
sub-04.zip
sub-05.zip
sub-06.zip

To loop it (I assume you're executed your bash script from the directory, where all files which are going to be modified are located):

for i in `ls -w1`
do
 sed -ie "s/Nora\ Hmann\ \-\ //g" /path/to/$i
done
13dimitar
  • 1,529
1

Another solution by using cut and sed:

cat filename.txt | cut -d'-' -f2- |  sed 's/^ //g'

Another one only with cut (and only in case of the names are composed by first name and surname separated by space):

cat filename.txt | cut -d' ' -f4-
1

With bash or ksh, assuming the files are in the current directory:

for name in ./"Nora Hmann - sub-"??.zip; do
  newname="./${name##* }"
  mv -i "$name" "$newname"
done

The parameter expansion ${name##* } will remove everything from $name up until (and including) the last space character.

If the name at the start of the filename is not fixed, then the following will grab all Zip files and try to rename them in the same manner:

for name in ./*.zip; do
  newname="./${name##* }"
  mv -i "$name" "$newname"
done

I've opted for using mv -i in these examples so that you interactively have to confirm any renaming that would overwrite an already existing file.

Another way to do it would be to simply skip files that would otherwise overwrite existing files:

for name in ./*.zip; do
  newname="./${name##* }"
  if [[ ! -e "$newname" ]]; then
    mv "$name" "$newname"
  else
    printf 'Not renaming "%s", new filename exists\n' "$name" >&2
  fi
done
Kusalananda
  • 333,661
0

If you don't mind losing the second -, another solution is using tr:

x='Nora Hmann - sub-01.zip'
printf '%s\n' "$x" | tr -d 'NoraHmn -'
sub01.zip

Note this relies on the fact that the characters in the set 'NoraHmn -' do not intersect with sub*.zip.

Kusalananda
  • 333,661
Lee
  • 495