LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


Reply
  Search this Thread
Old 08-22-2006, 08:12 AM   #1
wendtbd
LQ Newbie
 
Registered: Aug 2006
Posts: 14

Rep: Reputation: 0
SED and MV question


I am a Linux newbie and have a question about scripting using SED and MV. Below is a set of command I am running and would like to combine into one if possible.

for i in *; do cat "$i"|sed -e 's/ns3.domain.com./ns1.newdomain.com./' >>"$i".new; done

for i in *; do cat "$i"|sed -e 's/ns4.domain.com./ns2.newdomain.com./' >>"$i".new; done

Second I am trying to overwrite the original files with the .new files I am creating above but can't figure out how to remove the .new extension. An example of what I want to do, in a for loop of course, is below.

mv zone.dns.new zone.dns

Here is what I have which doesn't work of course.

for i in *; do mv "$i" "$i"; done

Any suggestions would be appreciated,

Barrett
 
Old 08-22-2006, 08:35 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Hello and welcome to LQ, hope you like it here.

Combine like this?:
Code:
#!/bin/sh
for i in *; do cat "$i"|sed -e 's/ns3.domain.com./ns1.newdomain.com./' \
-e 's/ns4.domain.com./ns2.newdomain.com./' >>"$i".new; mv "$i.new" "$i" 
done
# always clean exit a script
exit 0
BTW, you don't want this:
Code:
for i in *; do
but something similar to:
Code:
find /some/dir -type f -name "domain*.conf"|while read i; do
 
Old 08-22-2006, 09:01 AM   #3
wendtbd
LQ Newbie
 
Registered: Aug 2006
Posts: 14

Original Poster
Rep: Reputation: 0
Yeah, looks like a great forum and a great place for a newbie to learn a lot...

I guess when you combine all the commands it is pretty easy to overwrite the original file. I have ran this and it worked great, thanks for the help.

Just for future reference how would I do this with the mv command by itself? Using the command;

for i in *; do mv "$i" "$i"; done

Can I add something to the second "$i" to remove (in my case) the .new extension? I will use the find /some/dir code you suggested above from now on, seems a little safer way to go.

Thanks again
Barrett
 
Old 08-22-2006, 02:46 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Can I add something to the second "$i" to remove (in my case) the .new extension?
Maybe something like:
Code:
find /some/dir -type f -name "*.new"|while read i; do
mv "$i" "${i%.new}"; done
 
Old 08-23-2006, 11:22 AM   #5
wendtbd
LQ Newbie
 
Registered: Aug 2006
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks again unSpawn, this works great.
 
  


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
Another SED question 3saul Linux - Software 2 01-28-2007 12:06 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed question provkitir Programming 4 05-03-2005 09:41 AM
sed question from Ch 6.12 na5m Linux From Scratch 1 02-02-2005 03:36 PM
sed question dolvmin Linux - Software 4 03-09-2004 09:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 07:23 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