The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
mget * (obtein files from current directory but not the files form sub-directories) Peter321 Shell Programming and Scripting 0 03-12-2009 11:59 AM
Merge files of differrent size with one field common in both files using awk shashi1982 Shell Programming and Scripting 2 03-03-2009 07:12 AM
I need a script to find socials in files and output a list of those files NewSolarisAdmin Shell Programming and Scripting 1 02-19-2009 01:01 PM
Find duplicates from multuple files with 2 diff types of files ricky007 Shell Programming and Scripting 2 03-04-2008 01:46 PM
text files, ASCII files, binary files and ftp transfers Perderabo Answers to Frequently Asked Questions 0 04-08-2004 05:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-22-2009
timgolding timgolding is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 34
sed has zeored my files. Help me with sed please

i made a script to update a lot of xml files. to save me some time. Ran it and it replaced all the the files with a 0kb file. The problem i was having is that I am using sed to change xml node <doc_root>. The problem with this is it has a / in the closing xml tag and the stuff inside will also have a bunch of / because it is a file path. so i am using sed and was hoping this would work:

Code:
sed 's/<doc_root><\/doc_root>/<doc_root>\/'$i'\/web<\/doc_root>/g' $my_file > $my_file
Is my problem in the regex or is it because I am using $my_file > $my_file ? do i need to copy it to a temp file first the mv it to old file?
Or if its the regex can someone help with that
Here is the search string
Code:
<doc_root></doc_root>
Here is a typical replace string
Code:
<doc_root>/domain.co.uk/web</doc_root>
where the daomin.co.uk will be an argument from a for loop i.e $i

Please help before my boss gets angry with me
  #2 (permalink)  
Old 04-22-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Quote:
Originally Posted by timgolding View Post
i made a script to update a lot of xml files. to save me some time. Ran it and it replaced all the the files with a 0kb file. The problem i was having is that I am using sed to change xml node <doc_root>. The problem with this is it has a / in the closing xml tag and the stuff inside will also have a bunch of / because it is a file path. so i am using sed and was hoping this would work:

Code:
sed 's/<doc_root><\/doc_root>/<doc_root>\/'$i'\/web<\/doc_root>/g' $my_file > $my_file
Is my problem in the regex or is it because I am using $my_file > $my_file ? do i need to copy it to a temp file first the mv it to old file?
Or if its the regex can someone help with that
Here is the search string
Code:
<doc_root></doc_root>
Here is a typical replace string
Code:
<doc_root>/domain.co.uk/web</doc_root>
where the daomin.co.uk will be an argument from a for loop i.e $i

Please help before my boss gets angry with me

I will look into the function of the sed but you are redirecting the output to the same file name, you shud never do that. that is why u have 0kb file.

redirect it to a temp file
verify the result
mv tempfile filename


cheers,
Devaraj Takhellambam
  #3 (permalink)  
Old 04-22-2009
timgolding timgolding is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 34
Thanks alot guys that works
  #4 (permalink)  
Old 04-22-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Code:
sed "s#<doc_root></doc_root>#<doc_root>/$i/web</doc_root>#g" $my_file > /tmp/myTemp && mv /tmp/myTemp $my_file
OR
Code:
{ rm $my_file; sed "s#<doc_root></doc_root>#<doc_root>/$i/web</doc_root>#g" > $my_file; } < $my_file

Last edited by vgersh99; 04-22-2009 at 12:45 PM.. Reason: alternative
  #5 (permalink)  
Old 04-22-2009
timgolding timgolding is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 34
cool lots of variations. This also works

Code:
sed 's/<doc_root><\/doc_root>/<doc_root>\/'$i'\/web<\/doc_root>/g' $my_file > "tmp/"$i".xml"
mv "tmp/"$i".xml" $my_file
no need for so many quotes.
Also you need to double quote "$i" in case a file name has embedded quotes:
Code:
sed 's/<doc_root><\/doc_root>/<doc_root>\/'"$i"'\/web<\/doc_root>/g' $my_file > "tmp/$i.xml"
mv "tmp/$i.xml" "$my_file"
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0