Hi Everyone,
I need a replace a string (ex : this) with the string (ex : that) in each line of a file (sample.txt) using a C Shell Script and I am not allowed to use sed,awk and replace command in the unix , So i tried to use tr but it is replacing the character-by-character so if want anyone to help in with these.

Thanks in advance.

Thanks
Srikanth M.

See if this works for you

#!/bin/csh

rm ./tmp.txt >& /dev/null
foreach line ( "`cat sample.txt`" )
   set line = "$line:gas/this/that/"
   echo $line >> ./tmp.txt
end  
mv ./tmp.txt sample.txt

Hi histrungalot,
Thanks for your reply, but it works only if there is a single word in each line (sample.txt) so if has more than single word then it does not work properly as space is a delimiter for word list variables. So if there is any other way ??

Thanks
Srikanth M.

See if this works for you

#!/bin/csh

rm ./tmp.txt >& /dev/null
foreach line ( "`cat sample.txt`" )
   set line = "$line:gas/this/that/"
   echo $line >> ./tmp.txt
end  
mv ./tmp.txt sample.txt

Post a specific example of the search/replace string and sample.txt so I can see what you mean.

Hi histrungalot,

It is not working when it is given as
set line = "echo $line:gas/$search/$replace/"
in the script , output is not getting effected and also it is not showing any error.

Thanks
Srikanth M.

Post a specific example of the search/replace string and sample.txt so I can see what you mean.

set line = "echo $line:gas/$search/$replace/"

You need to execute the echo command and to do that you need to use the back ticks not quotes.

#!/bin/csh

foreach line ( "`cat sample.txt`" )
   # This works for me, you need the back ticks 
   set line = `echo "$line:gas/this/that/"`
   # Now line has the new values, so check it out
   echo $line
end

Hi histrungalot,
This is the piece of code is not working , but when it replace the variables "$search" and "$replace" with some other text then it is working , but i want it to be more user specific so how should i specify it ???

#!/bin/tcsh -f

rm ./tmp124.txt

echo -n "Enter Search String : "
set search = $<
echo -n "Enter Replace String : "
set replace = $<

foreach line ("`cat sample.txt`")
	set line = "`echo $line:gas/$search/$replace/`"
	echo $line >> ./tmp124.txt
end

mv ./tmp124.txt sample.txt

unset search replace count line

You need to execute the echo command and to do that you need to use the back ticks not quotes.

#!/bin/csh

foreach line ( "`cat sample.txt`" )
   # This works for me, you need the back ticks 
   set line = `echo "$line:gas/this/that/"`
   # Now line has the new values, so check it out
   echo $line
end

Not sure why this is causing problems, try this, looks weird but works for me.

#!/bin/tcsh -f

rm ./tmp124.txt >& /dev/null

echo -n "Enter Search String : "
set search = $<
echo -n "Enter Replace String : "
set replace = $<


foreach line ("`cat sample.txt`")
        set ss = ":gas/${search}/${replace}/"
        set re = `echo -n 'set line = "$line';echo -n "$ss";echo '"'`
        eval "$re"
        echo $line >> ./tmp124.txt
end

mv ./tmp124.txt sample.txt

unset search replace count line

Input:
% cat sample.txt
replace dog with cat and
make sure that dogs are not
in the file that has dogs and more dogs
because dogs are to be replaced with cat
Run command:
% ./c.sh
Enter Search String : dog
Enter Replace String : cat
Output:
% cat sample.txt
replace cat with cat and
make sure that cats are not
in the file that has cats and more cats
because cats are to be replaced with cat

Hi histrungalot,
Thank you for the post , it is working but as you said it is weird so trying in another method .

Thanks
Srikanth M.

Not sure why this is causing problems, try this, looks weird but works for me.

#!/bin/tcsh -f

rm ./tmp124.txt >& /dev/null

echo -n "Enter Search String : "
set search = $<
echo -n "Enter Replace String : "
set replace = $<


foreach line ("`cat sample.txt`")
        set ss = ":gas/${search}/${replace}/"
        set re = `echo -n 'set line = "$line';echo -n "$ss";echo '"'`
        eval "$re"
        echo $line >> ./tmp124.txt
end

mv ./tmp124.txt sample.txt

unset search replace count line

Input:
% cat sample.txt
replace dog with cat and
make sure that dogs are not
in the file that has dogs and more dogs
because dogs are to be replaced with cat
Run command:
% ./c.sh
Enter Search String : dog
Enter Replace String : cat
Output:
% cat sample.txt
replace cat with cat and
make sure that cats are not
in the file that has cats and more cats
because cats are to be replaced with cat

When I say weird, I just didn't think that it would have to look like that. It works and that is the goal.
With shells there is always many ways to do what you want. Some easy and some do the same thing but the hard way. This looks like the hard way but I was unable to find the easy way using tcsh. This was the only way, for now.
If you find an easier way please post it.

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.