LinuxQuestions.org
Review your favorite Linux distribution.
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 07-11-2007, 05:21 PM   #1
gr8scot
Member
 
Registered: Jun 2007
Distribution: Debian, kubuntu
Posts: 73

Rep: Reputation: 16
Question 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.
 
Old 07-11-2007, 05:31 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I would use Krename. I love that utility for making changes in massive numbers of file names if they have similar names.
 
1 members found this post helpful.
Old 07-11-2007, 05:42 PM   #3
gr8scot
Member
 
Registered: Jun 2007
Distribution: Debian, kubuntu
Posts: 73

Original Poster
Rep: Reputation: 16
Thumbs up Thanks stress junkie, I'll check that out.

I've never heard of krename. Good info.
 
Old 07-11-2007, 08:28 PM   #4
gr8scot
Member
 
Registered: Jun 2007
Distribution: Debian, kubuntu
Posts: 73

Original Poster
Rep: Reputation: 16
Thanks stress junkie

krename rocks the casbah!
 
Old 07-12-2007, 05:46 AM   #5
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by gr8scot
krename rocks the casbah!
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.

Last edited by stress_junkie; 07-12-2007 at 05:48 AM.
 
Old 07-12-2007, 08:04 AM   #6
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
1 members found this post helpful.
Old 07-12-2007, 08:13 PM   #7
gr8scot
Member
 
Registered: Jun 2007
Distribution: Debian, kubuntu
Posts: 73

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

Last edited by gr8scot; 02-20-2011 at 10:29 PM. Reason: fix open quote tag
 
  


Reply

Tags
awk, bash, bourne, script, sed, shell



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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
read line by line form text file in java. spank Programming 1 10-18-2006 02:46 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM
[C & Linux] Read line from text-file Y_Haarman Programming 5 11-08-2002 04:44 PM

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

All times are GMT -5. The time now is 03:33 PM.

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