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.

2
  • 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
    Commented Jan 10, 2017 at 13:20
  • also, can you clarify you want to rename filenames or is this text processing of file contents?
    – Sundeep
    Commented Jan 10, 2017 at 13:43

5 Answers 5

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/^.* //' *
0
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
11
  • but how to use the command. When tried it says sed: no input file Commented Jan 10, 2017 at 13:40
  • replace stack with the name of the file, which you're about to modify.
    – 13dimitar
    Commented Jan 10, 2017 at 13:48
  • the issue is, when i give batch command for f in Nora*.zip; do sed command $f; done; given files names have spaces in between, sed throws away error as it tries to rename files as follows: sed -ie "s/Nora\ Hmann\ \-\ //g" Nora Commented Jan 10, 2017 at 14:01
  • ok, if your script indeed is: for f in ls -w1 Nora*.zip do sed -ie "s/Nora\ Hmann\ \-\ //g" $f done
    – 13dimitar
    Commented Jan 10, 2017 at 14:02
  • 1
    Check my answer again.
    – 13dimitar
    Commented Jan 10, 2017 at 14:15
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
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .