Using C-Shell Scripting for Substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using C-Shell Scripting for Substitution
# 1  
Old 12-04-2010
Using C-Shell Scripting for Substitution

mv myFile.txt myFile.txt.bak
sed s/foo/bar/g myFile.txt.bak > myFile.txt

Turn the two-line version, above, of the substitution commands into a shell script, subst1 taking three parameters:
the string to be replaced
the string with which to replace it
the name of the file in which to make the substitution
For example,


~/UnixCourse/scriptAsst/subst1 foo bar myFile.txt
should replace all occurrences of "foo" in the file myFile.txt by "bar", leaving the original file as myFile.txt.bak.

Similarly,


~/UnixCourse/scriptAsst/subst1 abc "" aardvark.dat
should remove (replace by the empty string) all occurrences of "abc" in the file aardvark.dat by, leaving the original file as aardvark.dat.bak.

One problem with this approach is that the file's creation and modification dates are altered, even if no change was made to the file contents (because the string being relaced did not occur within the file).

Create a new script, subst2, taking the same three parameters, that performs the substitution and backup file creation when the target string occurs somewhere in the file, but leaves the file completely unchanged (and does not create the .bak file) if the string being replaced is nowhere in the file. (Hint: there are two ways to do this. One is to check first for the presence of the target string and, if it is found, to do the two-step substitution as above. On average, though, it may be faster to go ahead and do the substitution, and then to check and see if the resulting files are identical.)

Generalize your subst2 script, producing a new script subst that will apply a substitution to any number of files given on the command line. For example

~/UnixCourse/scriptAsst/subst foo bar myFile1.txt myFile2.txt myFile3.txt
should apply the same substitution throughout each of the three files named there.

Below is the script I started writing, but I'm not exactly sure how to get the script to make the substitution of the test file.


#!/bin/csh
set string1=$find
set string=$replace
set fileSub=$file


sed "s/$find/$replace/g" $3

and when I give the command ~/UnixCourse/scriptAsst/subst1 and or test1.txt it gives me an error message saying that find is undefined. I am lost with this and any help will be very much appreciated.

---------- Post updated at 11:39 AM ---------- Previous update was at 11:30 AM ----------

This is part of an assignment that I am trying to figure out and I got stuck at this particular part.
# 2  
Old 12-04-2010
Wrong subforum , you should post on Homework & Coursework Questions.
Thread reported.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Escape and command substitution in scripting

I have one question in shell script for escape "\" with command substitution "` `". I post there to seek help to understand how it works. My original one piece of code in script like this: This piece of code in whole script is working without errors chk_mode=`sqlplus -s /nolog<<EOF connect /... (4 Replies)
Discussion started by: duke0001
4 Replies

2. Shell Programming and Scripting

Bad substitution error in shell script

I have script data.sh which has following error. Script Name : data.sh #!/bin/sh infile=$1 len=${#infile} echo $len texfile=${infile:0:$len-4} echo $texfile run command ./data.sh acb.xml I get following error message: (5 Replies)
Discussion started by: man4ish
5 Replies

3. Shell Programming and Scripting

Help with substitution in shell script

Hello, I Have a file like this, with five columns: B D F T L --------- 0 0 0 0 0 0 0 0 1 0 0 2 0 0 1 I need, for each row, to append another five columns similar to the first ones, but with the field labeled as "T" (4th field of the column) substituted in this way: (16 * value of... (3 Replies)
Discussion started by: PapaJustify
3 Replies

4. Shell Programming and Scripting

Shell $,/r getting added in echo on variable substitution.

Hi All, I'm trying to run a similar script to copy a files from one location to another. #!/bin/bash source="/home/pradeepk/a.txt" destination="/home/pradeepk/dir1" cp $source $destinationi'm getting following error. cp: cannot stat `/home/pradeepk/a.txt\r': No such file or directorywhen... (1 Reply)
Discussion started by: pradeep2002gs
1 Replies

5. UNIX for Dummies Questions & Answers

sed substitution and shell variables

Hello!! Am trying to substitute the value of a shell variable with the value of another shell variable. The values are obtained into the shell variables through some other processing. for ex. i've used the follow sed command.. sed "s/$var1/$var2/g" i've also tried the other... (5 Replies)
Discussion started by: a_ba
5 Replies

6. Shell Programming and Scripting

Shell scripting substitution techniques

Hi guys, I'm looking for some thoughts on this. I'm trying to do a clean 1 liner to substitute some values. What I have: sed 's/Personid=.*/Personid=xxxxxx/' $tmpFileOut sed 's/Person:.*/Person:xxxxxx/' $tmpFileOut sed 's/PersonID:.*/PersonID: xxxxxx/' $tmpFileOut Obviously that's a bit... (1 Reply)
Discussion started by: rich@ardz
1 Replies

7. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

8. Solaris

Shell variables substitution in a file

Hi, I have to read a file and translate the contents including substituting the variables if any and write to another file without using sed or awk. For ex:- inputfile.txt ----------- servername=$SERVER application=$APPL outputfile.txt ------------ servername=actual server name... (2 Replies)
Discussion started by: axes
2 Replies

9. UNIX for Advanced & Expert Users

shell variables and sed substitution

My specific goal: automatically edit a Makefile variable's (EXTRAVERSION) value in a kernel Makefile. So, I have a shell script that takes one parameter, a version string: buildkernel.sh 2.6.18.21.7-custom In the script, I assign the parameter to K_VER: K_VER="2.6.18.21.7-custom"... (2 Replies)
Discussion started by: duderonomy
2 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question