We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,011 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Replace String in a C Shell Script

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.

2
Contributors
9
Replies
1 Week
Discussion Span
1 Year Ago
Last Updated
11
Views
Question
Answered
msrikanth
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
histrungalot
Posting Whiz in Training
280 posts since May 2008
Reputation Points: 76
Solved Threads: 36
Skill Endorsements: 1

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
msrikanth
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

histrungalot
Posting Whiz in Training
280 posts since May 2008
Reputation Points: 76
Solved Threads: 36
Skill Endorsements: 1

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.

msrikanth
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
histrungalot
Posting Whiz in Training
280 posts since May 2008
Reputation Points: 76
Solved Threads: 36
Skill Endorsements: 1

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
msrikanth
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

histrungalot
Posting Whiz in Training
280 posts since May 2008
Reputation Points: 76
Solved Threads: 36
Skill Endorsements: 1

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

msrikanth
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

histrungalot
Posting Whiz in Training
280 posts since May 2008
Reputation Points: 76
Solved Threads: 36
Skill Endorsements: 1
Question Answered as of 1 Year Ago by histrungalot

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0818 seconds using 2.68MB