Help with word substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with word substitution
# 1  
Old 04-28-2011
Help with word substitution

Hi all,
I am trying to write a script substituting one word in a particular file with another word (sed) but I'm having trouble creating the backup file. The following are my instructions:
The Unix program sed is useful for making simple substitutions throughout an entire file. But one of its limitations is that it always puts its results onto standard output and therefore it cannot be used to edit a file "in place". For example, the command
sed s/foo/bar/g myFile.txt
will replace every occurrence in myFile.txt of the string "foo" by the string "bar", with the altered file appearing at the standard output (i.e., scrolling by on your terminal screen). But what if you really wanted to make this change to myFile.txt? You can't do this:
sed s/foo/bar/g myFile.txt > myFile.txt
because the output redirection begins by creating an empty output file myFile.txt, thus destroying the input file before we get achance to read it. Instead, we wind up doing this in two or more steps: mv myFile.txt myFile.txt.bak
sed s/foo/bar/g myFile.txt.bak > myFile.txt
In software development, however, substitutions like this often need to be made to many different files. For example, a decision to change a variable name may affect several different source code files. This kind of substitution is common enough that it's worth developing a script to streamline the process.
  1. Create a directory ~/UnixCourse/scriptAsst.
  2. Turn the two-line version, above, of the substitution commands into a shell script, subst1 taking three parameters:
    1. the string to be replaced
    2. the string with which to replace it
    3. 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.
  3. 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.)
  4. 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.
I am having a very difficult time with this assignment. Here is what I have for subst1:

#!/bin/sh
find=$1
replace=$2
test=$3
mv $test $test.txt.bak
sed s/$find/$replace/g $test.txt.bak > $test

Also, I am unsure how to do part 3 and part 4. I was wondering if part 4 consisted of using the following in order to apply the substitution to any number of files given on the command line:
#!/bin/sh
for x in $*
do
echo $x
done

Am I on the right track? Any help at all will be greatly appreciated. I am so stressed about this problem!Smilie

Last edited by jim mcnamara; 04-28-2011 at 02:30 PM.. Reason: Homework question, please repost in homework forum
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. UNIX for Dummies Questions & Answers

Word-counting and substitution with awk

Hi!! I am trying to write a program which allows me to count how many times I used the same word in a text: {$0 = tolower ($0) gsub (/_]/, "", $0) for (i = 1; i <= NF; i++) freq++ } END { for (word in freq) printf "%s\t%d\n", word, freq It seems work but... (3 Replies)
Discussion started by: ettore8888
3 Replies

3. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

4. Shell Programming and Scripting

whole word substitution in SED

I am trying to substitute something with sed and what I want is to substitute a whole word and not part of a word. ie sed 's/class/room/g' filename will substitute both class and classes into room and roomes which is not what i want Grep for instance can use the -w option or <> grep -w... (7 Replies)
Discussion started by: gikay01
7 Replies

5. Linux

Multiple files and word substitution

Hi , I have bunch of sql file which contain UHCDEV01 in them . I want to replace all the UHCDEV01 with UHCETL01 in all the files. I have written this code which shows correct output on sh -x but doesn't change the output file . #cat change_dbname.shl #!/bin/ksh... (1 Reply)
Discussion started by: capri_drm
1 Replies

6. Linux

Last word substitution

Ok this is last question of the day from my side . I have this file and I want to replace the last letter " , " with " ) " . The input file is #cat zip.20080604.sql CONNECT TO TST103 ; SET SESSION_USER OPSDM001 ; SET CURRENT SCHEMA OPSDM001 ; CREATE VIEW OPSDM001.vw_zip SELECT ( ... (4 Replies)
Discussion started by: capri_drm
4 Replies

7. Linux

word substitution in unix

Hi I am trying to substitute 2 words on the same line with _S02 as suffix. Like this . IN "TSOPS09" INDEX IN "TSOPIX09" ; to IN "TSOPS09_S02" INDEX IN "TSOPIX09_S02" ; i used the following code to make the change , it works fine for first substitution not the second one . ... (6 Replies)
Discussion started by: capri_drm
6 Replies

8. Shell Programming and Scripting

How to use sed substitution using a $variable for a line containing a word

$ cat ggg /E*Fare/testteam/public/pf3/nggfstatic/4k-pf3_3.case REGION1: /E*Fare/dist/src/nggfstaticbase/EFare/Server CODEBASE1: /dev_tools/LINUXMTP-4/EFS070718E/EFare/Server DATABASE1: nggfstatic SCRIPT: /efare1/admin/ezlcn/scripts/pf3_3_scriptlist.input PROLOGINITSIZE not yet set You... (4 Replies)
Discussion started by: Sangal-Arun
4 Replies

9. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies

10. Shell Programming and Scripting

word substitution in csh

I have data that looks something like this: term1/term2/2005-12-01 13:20:30/term4 I need to make it look like this: term1/term2/20051201132030/term4 I am using a csh script. I have tried to do it by first converting the date/time to the format in which I want it, and then replacing it... (1 Reply)
Discussion started by: oprestol
1 Replies
Login or Register to Ask a Question