LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-07-2014, 12:24 AM   #1
Mr_Fixit
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Rep: Reputation: 0
Question 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?
 
Old 01-07-2014, 11:48 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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)

Last edited by syg00; 01-08-2014 at 12:11 AM. Reason: escape spaces
 
Old 01-08-2014, 01:21 PM   #3
Mr_Fixit
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
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 ? & ( ) ' ; , -

Last edited by Mr_Fixit; 01-08-2014 at 01:37 PM. Reason: clean up
 
Old 01-08-2014, 03:55 PM   #4
Mr_Fixit
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
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.

Last edited by Mr_Fixit; 01-08-2014 at 07:19 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
[SOLVED] How to changing same file names by their directory names? bayaraa_u Linux - General 3 04-09-2010 08:26 AM
grep+awk+sed+paste+sort in one script? mchriste Linux - Software 13 03-05-2009 01:57 PM
Script: SED for Copy/Paste Lines from Files unihiekka Programming 2 10-07-2008 06:12 AM
using sed ( I think ) to extract names from e-mail address rosv Programming 4 07-07-2008 02:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration