LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Cleaning up file names with paste, mv and/or sed? (https://www.linuxquestions.org/questions/linux-general-1/cleaning-up-file-names-with-paste-mv-and-or-sed-4175490391/)

Mr_Fixit 01-07-2014 12:24 AM

Cleaning up file names with paste, mv and/or sed?
 
Been a while since I've been on here. So I have a poorly labeled file collection. A friend wanted to see what I had so i
Code:

:~/ ls ~/files/ > badfiles.txt
They reviewed and edited the file names and sent it back as goodfiles.txt. Now both files correspond line by line but there are no patterns to the editing.
Code:

badfiles.txt          goodfiles.txt
  FILE NAME1.XXX        file name 1
  file_name 2.YYY      file name 2
  file.name3.XXX        file name 3
  filename(4).ZZZ      file name 4

So I ideally want to rename the files but using badfiles.txt as input and goodfiles.txt as output.

I tried this with unintended results
Code:

while read -r a b; do ! [[ -e "$b" ]] && mv "$a" "$b"; done <<< "$(paste badfiles.txt goodfiles.txt)"
and
Code:

paste badfiles.txt goodfiles.txt | while read n k; do mv -T $n* $k; done
and several variations of the above. The goodfiles.txt has been stripped of extensions as well so is it possible to preserve them during the renaming?

syg00 01-07-2014 11:48 PM

Try parameter substitution to get the extensions
Code:

paste badfiles.txt goodfiles.txt | while read n k ; do ext=${n##*.} ; mv $n $k.$ext ; done
You're going to have to escape the spaces goodfiles.txt first - something like this might do it
Code:

sed 's/ /\\ /g' goodfiles.txt
(presumes no extranious blanks - especially at end of line)

Mr_Fixit 01-08-2014 01:21 PM

Thank you for the reply! I knew I had the right utilities, just wasn't sure how to define the variables as needed.

Now do I need to escape anything else? Specifically, some names include ? & ( ) ' ; , -

Mr_Fixit 01-08-2014 03:55 PM

So escaping everything came up with some errors..

Code:

sed '
s/[/\\[/g
s/]/\\]/g
s/-/\\-/g
s/&/\\&/g
s/{/\\{/g
s/}/\\}/g
s/(/\\(/g
s/)/\\)/g
s/;/\\;/g
s/ /\\ /g'
<../badfiles.txt >../newbadfiles.txt

I checked the output and the whitespace was escaped.

I was testing this with:
Code:

$ paste ../newbadfiles.txt | while read n ; do touch $n ; done
The output was each string was made into a file even with escaped characters. I then tried with double quotes
Code:

's/.*/"&"/'
Touch still manages to split the file at the whitespace and some special characters.


All times are GMT -5. The time now is 08:49 AM.