LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   use awk &/or sed to read file 1 line 1 & file 2 line 1 (http://www.linuxquestions.org/questions/linux-general-1/use-awk-and-or-sed-to-read-file-1-line-1-and-file-2-line-1-a-568530/)

gr8scot 07-11-2007 06:21 PM

use awk &/or sed to read file 1 line 1 & file 2 line 1
 
I'm having a beast of a time trying to reference line1 of two different files in the same command from a small shell script. The goal is to end up with the following sent to the shell:
mv "stupid filename with spaces copied from Windows.mp3" less_stupid_filename.mp3

I have sed doing the search & replace algorithm just fine, but the closest I've gotten to automating the rename of entire directories of my music collection by shell script is the above command, separated by a newline.

For some reason, BASH is not friendly to that format!

;)

Any general tips will be appreciated. If you care to look at the existing script to troubleshoot, just ask.

stress_junkie 07-11-2007 06:31 PM

I would use Krename. I love that utility for making changes in massive numbers of file names if they have similar names.

gr8scot 07-11-2007 06:42 PM

Thanks stress junkie, I'll check that out.
 
I've never heard of krename. Good info.

gr8scot 07-11-2007 09:28 PM

Thanks stress junkie
 
krename rocks the casbah!
:D

stress_junkie 07-12-2007 06:46 AM

Quote:

Originally Posted by gr8scot
krename rocks the casbah!
:D

I'm very glad that you like it. I often use it after downloading vast numbers of jpeg files from NNTP news groups. (What can I say? I love actresses. :)) I have found that it is worth using Krename if you have as few as five files to rename as compared to using a file manager and renaming each file individually by hand.

timmeke 07-12-2007 09:04 AM

If you already have the sed command worked out, maybe you could try the next loop (in bash)?
Code:

IFS="\n";
for fromFile in `cat list_of_files_with_spaces`; do
toFile=`echo ${fromFile} | your_sed_command`;
mv "${fromFile}" "${toFile}";
done;

You may want to echo the "mv" command before actually executing it, just to make sure that it does what you expect it to do.

Alternatively, say you have the file names with spaces in one file and the sed'ed file names in another, then you can use "paste" utility to combine both files (it kind of concatenates line by line), using an easy field separator like ',' or so.
Then you can go over that file in a loop (again, line-wise), each time using 'cut' to get both fields into a variable. Or simply use "sed" directly to create the mv commands.
Example:
Code:

file1=/path/to/some/file #file1 contains the win-style file names with spaces
file2=/path/to/2nd/file #file2 contains the "corrected" file names
#The first sed adds the "mv" command and an opening double quote in front,
#the second replaces the ',' delimiter with the closing double quote and adds a space for the mv command
paste --delimiters=, file1 file2 | sed -e 's/^./mv "&/' | sed -e 's/,/" /' > yourScript

After this, examine the results in file yourScript. If they're OK, make the script executable and run it.

gr8scot 07-12-2007 09:13 PM

That looks like exactly what I want
 
Quote:

Originally Posted by timmeke
If you already have the sed command worked out, maybe you could try the next loop (in bash)?

Code:

IFS="\n";
for fromFile in `cat list_of_files_with_spaces`; do
toFile=`echo ${fromFile} | your_sed_command`;
mv "${fromFile}" "${toFile}";
done;

Quote:

Originally Posted by timmeke
You may want to echo the "mv" command before actually executing it, just to make sure that it does what you expect it to do.

I've already re-named all the files with krename, but it did have trouble with directories. Possibly by design. Even when started from a terminal as root, it wouldn't change the name of any directory with any contents. Thanks.


All times are GMT -5. The time now is 11:41 AM.