LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-10-2010, 12:55 AM   #1
tjay1983
LQ Newbie
 
Registered: Aug 2010
Posts: 12

Rep: Reputation: 0
rename multiple files


Hi

ls -lart *.ddl

a.ddl
b.ddl
c.ddl
..
..
..
..
..



What I want to do is, I want to open each above files (from ls -l *.ddl) and do the rename like below

%s/"DATA_INDEX"/tablespace


How can I do that in shell script?
 
Old 08-10-2010, 01:01 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Code:
for FILE in *.ddl ; do 
    sed -i -e 's/DATA_INDEX/tablespace/' "$FILE"
done
If you want to replace more matches in a line, also add 'g' at the end of the expression:
Code:
s/DATA_INDEX/tablespace/g
---- Edit ----

Wait you're referring to the filename and not contents the right? For renaming it should be:

Code:
for FILE in *.ddl ; do 
    mv "$FILE" "${FILE//DATA_INDEX/tablespace}"
done

Last edited by konsolebox; 08-10-2010 at 01:12 AM.
 
Old 08-10-2010, 01:33 AM   #3
tjay1983
LQ Newbie
 
Registered: Aug 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Hi

I want to rename contents in each file
I tried your option but no luck: (HP-unix)

sed: illegal option -- i
Usage: sed [-n] [-e script] [-f source_file] [file...]
sed: illegal option -- i
Usage: sed [-n] [-e script] [-f source_file] [file...]
 
Old 08-10-2010, 01:43 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
You're probably using an old version of sed that doesn't support '-i'. Here's a work around for that.
Code:
TEMPFILE=__TEMPFILE__

for FILE in *.ddl ; do
	echo "Changing the contents of $FILE."

	sed -e 's/DATA_INDEX/tablespace/' "$FILE" > "$TEMPFILE" || {
		echo "Failed to change the contents of $FILE."
		break
	}

	cat "$TEMPFILE" > "$FILE" || {
		echo "Failed to save the modified contents from temp file $TEMPFILE to $FILE."
		break
	}

	# Instead of cat, we can also use mv but it may have problems with permissions and/or time stamps
	# mv "$TEMPFILE" "$FILE"
done

rm "$TEMPFILE"
Please test it first before actually using it. e.g. backup the files first

Last edited by konsolebox; 08-10-2010 at 01:45 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename multiple files in Bash damianpfister Programming 5 11-15-2009 09:30 AM
Rename multiple files into indexed form tiaratiara Programming 1 02-27-2009 08:51 PM
How to rename multiple files with the same extension rogueeve Linux - Newbie 3 05-18-2007 02:54 AM
Rename multiple files in folders??? adds2one Linux - Software 19 10-05-2006 01:22 AM
How to rename multiple files? Rostfrei Linux - Newbie 3 07-11-2006 07:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:29 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration