Editing files using SED

Thread Solved

Join Date: Jan 2009
Posts: 3
Reputation: jinsonsani is an unknown quantity at this point 
Solved Threads: 0
jinsonsani jinsonsani is offline Offline
Newbie Poster

Editing files using SED

 
0
  #1
Jan 5th, 2009
I want to replace a text in a file eg (old_text) with another text eg (new_text) . new_text is a variable in my shell script. few methods that i tried ..
but this doesnt work
cat $line".sh" sed -e 's/abc/ABC/g' $line".sh"

sed 's/ordprg/new_string/g' $line".sh" > $line".sh"

Could some one can help me on this?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 1,763
Reputation: DimaYasny will become famous soon enough DimaYasny will become famous soon enough 
Solved Threads: 85
Moderator
Featured Poster
DimaYasny DimaYasny is offline Offline
Posting Virtuoso

Re: Editing files using SED

 
0
  #2
Jan 5th, 2009
in the first attempt, you need to add a pipe ('|') between the cat command and the sed command
Real stupidity always beats Artificial Intelligence. (Terry Pratchett)

BA BizMg, MCSE, DCSE, Linux+, Network+
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Editing files using SED

 
0
  #3
Jan 5th, 2009
Originally Posted by jinsonsani View Post
I want to replace a text in a file eg (old_text) with another text eg (new_text) . new_text is a variable in my shell script. few methods that i tried ..
but this doesnt work
cat $line".sh" sed -e 's/abc/ABC/g' $line".sh"

sed 's/ordprg/new_string/g' $line".sh" > $line".sh"

Could some one can help me on this?
You don't need cat to display the content of `$line".sh"' to sed, in order to substitute some text.

sed 's/ordprg/new_string/g' $line".sh" > $line".sh" redirecting to the same file you have opened, is the best way of corrupting your file or at best getting a empty file.
You need to redirect the output to another file and then rename it to your original filename. If you use the GNU sed, the -i option will do that for you automatically behind the scene.

sed 's/old_string/new_string/g' "$filename" > temp_file After that rename: mv temp_file "$filename"
Last edited by Aia; Jan 5th, 2009 at 7:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Editing files using SED

 
0
  #4
Jan 5th, 2009
Hey There,

Just a suggestion. If you're using Gnu Sed, you can use the -i option and it will do the changes inline (basically taking care of writing to a tmp file and then copying back, which Aia had suggested. As Aia noted, it's never a good idea to overwrite your original file with output from that same file)

That would boil your command line down to:

sed -i 's/old_string/new_string/g' "$filename"
That makes things easier to type Even so, whether you have the option or not, redirecting your sed output to a file and then copying back is the best method. Especially if you're not sure of the outcome, in which case you should just write your statement and see what you see on STDOUT or redirect into a temp file and look it over to see if it's doing exactly what you want/need.

Best wishes,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: jinsonsani is an unknown quantity at this point 
Solved Threads: 0
jinsonsani jinsonsani is offline Offline
Newbie Poster

Re: Editing files using SED

 
0
  #5
Jan 6th, 2009
Thanks a lot. temp_file works fine. but now i have a different problem . when i read a text from a file and create a new file using that text two small letters disappearing from the text and that leads to an invalid file path . two letters are "e"and "n". while reading from the file it fails to read these two letters . could you please tell me possible reason for this . is it taking those letters as some delimiter or something ?

Pre requesties

1)template.txt in the same path contains a text "ordprg"and a blank line

2)filenames.sh in the same path contains two lines
"ABCDEFJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrtuvwxyz"
Following program will eliminate "e" and "n"


#My Shell script is

processLine()
{
line="$@" # get all args

# F1=$(echo $line | awk '{ print $1 }')

#echo $line
#touch $line".sh"
#echo $line >> $line".sh"

#echo "date_stamp="'`'"date +%Y%m%d.%H%M%S"'`'" # used to date stamp all files for this run" >> $line".sh"

#fname="/home/jtest/"$line".sh"
#fname="./"$line".sh"

#echo $fname
#pname ="cp /home/jtest/test.txt /home/rms_user/jtest/$fname
#cp /home/jtest/test.txt $fname
#echo fname

}

### Main script stars here ###
# Store file name
FILE=""

# Make sure we get file name as command line argument
# Else read it from standard input device

if [ "$1" == "" ]; then
FILE="/home/jtest/filenames.sh"
#echo "k.txt"
else
FILE="$1"
#echo "1k.txt"
# make sure file exist and readable
if [ ! -f $FILE ]; then
#echo "2k.txt"
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE ]; then
#echo "3k.txt"
echo "$FILE: can not read"
exit 2
fi
fi
# read $FILE using the file descriptors

# Set loop separator to end of line
BAKIFS=$IFS
echo $IFS
IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<$FILE
while read line
do
# use $line variable to process line in processLine() function
processLine $line
fname="./"$line".sh"

#pnmae="/home/jtest/"
#echo $pname

echo $fname

cp /home/jtest/template.txt $fname

#echo /home/jtest/template.txt $fname


sed 's/ordprg/'$line".sh"'/g' $line".sh" > temp_file

mv temp_file $line".sh"

#echo "#filename" $line".sh" >> $line".sh"

done
exec 0<&3

# restore $IFS which was used to determine what the field separators are
BAKIFS=$ORIGIFS
exit 0

# Please Respond to this Thanks in Advance
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Editing files using SED

 
0
  #6
Jan 6th, 2009
Hey There,

I think your problem has to do with the "echo" that you're using (shell built-in vs. the binary). I'd guess that on this line, the echo doesn't accept the -e and -n arguments and is using them, literally, as IFS characters, which would result in your losing them:

IFS=$(echo -en "\n\b")

Hope that helps, and best wishes,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: jinsonsani is an unknown quantity at this point 
Solved Threads: 0
jinsonsani jinsonsani is offline Offline
Newbie Poster

Re: Editing files using SED

 
0
  #7
Jan 7th, 2009
Thats Great. I am obliged to you . This code works fine . I eliminated -en . hope it will not be harmful for me in the future.
RGDS
jinsonsanik@yahoo.com

Originally Posted by eggi View Post
Hey There,

I think your problem has to do with the "echo" that you're using (shell built-in vs. the binary). I'd guess that on this line, the echo doesn't accept the -e and -n arguments and is using them, literally, as IFS characters, which would result in your losing them:

IFS=$(echo -en "\n\b")

Hope that helps, and best wishes,

Mike
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Editing files using SED

 
0
  #8
Jan 7th, 2009
Great news!

Glad I could help you out

Best wishes,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC