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?

in the first attempt, you need to add a pipe ('|') between the cat command and the sed command

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"

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

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

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

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

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

Great news!

Glad I could help you out :)

Best wishes,

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.