Replace script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace script
# 1  
Old 07-15-2011
Replace script

I'm new to UNIX and I am trying to figure out how to get started on this. I need to write a script, “replace” that will process three or more command-line arguments as <search string>, <replace string>, and <file extension 1>, <file extension 2> … (one or more), to replace all occurrences of <search string> by <replace string> of all files with the given extensions: *.<file extension 1>, *.<file extension 2> … in the current directory. Temporary files may exist during execution but not at the end.

Assuming I do this correctly, I should get something like

Code:
$ ls *.txt *.dat
abc.txt xyz.dat
$ cat abc.txt
course number cs333
homework 4
$ cat xyz.dat
333 miles away.
33 is not a factor of 333.
$ ./replace 333 4444 txt dat
$ cat abc.txt
course number cs4444
homework 4
$ cat xyz.dat
4444 miles away.
33 is not a factor of 4444.

If I was just doing single line commands, replacing specific items wouldn't be that hard, but in a shell script, I'm sort of lost. I think I might want to use the user input and then do a grep search on the file and replace it? I have absolutely no idea.
# 2  
Old 07-15-2011
Is this homework? Because this can easily be done with sed.
# 3  
Old 07-15-2011
Seem like homework from the posters question. Let's close and reminder poster of the rule about this:

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Shell Programming and Scripting

Script to replace lines in ksh Script

Hi All, I am novice to Unix and I need your expert advice for the below task. There is a KSH script file in which I need to replace few line as per the below expectations. So my file look like as # Host Setup Command: Line 1 Line 2 Line 3 Line 4 Line Any... (6 Replies)
Discussion started by: rupid0609
6 Replies

3. Shell Programming and Scripting

Problem With Replace Script

Hi, I posted a topic requesting help with a script to replace certain things in an XML file https://www.unix.com/shell-programming-scripting/202943-replace-string-newline-2.html The replies helped a lot but I found that on big files it didn't work properly. The file I'm amending is... (5 Replies)
Discussion started by: Ste_Moore01
5 Replies

4. Shell Programming and Scripting

Search and replace script

Hi, Below is the script which will find a particular text and replace with another one in a group of files under a directory /test #!/bin/bash old=$1 --- first input old text new=$2--- input new text cd /test --- folder into which files need to be checked for y in `ls *`; do sed... (2 Replies)
Discussion started by: chetansingh23
2 Replies

5. Shell Programming and Scripting

Script to search and replace

Hi All, I am trying to write a script which will find a particular text in certain group of files under a directory and if found correctly it will replace them with a new text in all the files. Could any one let me know how do i find the text in many files under a directory. Thanks (3 Replies)
Discussion started by: chetansingh23
3 Replies

6. Linux

A find and replace script

Hello everyone, I am trying to do a simple script (shell or perl) for a bioinformatic problem, where I want to use a list from file2 and append this information to file1 (output). Example: File1 >Apple1 GAGACGATTTATATATAGAAGAGAG >Banana2 CAGAGAGAGAGACCCCCCCCCCCC File2 >Apple1 ... (2 Replies)
Discussion started by: herasj01
2 Replies

7. Shell Programming and Scripting

Replace line in files using script.

First sorry for my bad english. I have search the forum without finding anything I fully understand, so I ask here. The problem: I have a config file that I will chance onece a day. One line only. I know the linenumber, and I also know the start of the line. Line number 19, and the start... (3 Replies)
Discussion started by: Hempe
3 Replies

8. Shell Programming and Scripting

Shell Script to replace text

I need a little help with a shell script. I want to be able to specify parameters so that the script searches multiple files (specified as parameters) and only modifies the file if it finds the string of text. For the files it doesn't find the string of text within, it should leave it alone. ... (4 Replies)
Discussion started by: joebaber
4 Replies

9. Shell Programming and Scripting

script - replace text in file

I have a file something like this (except much larger amount of rows/data) 0001 blue testing1 0002 blue testing2 0006 blue testing3 0232 red testing4 2143 blue testing5 3453 blue testing6 In a script I want to replace the word red with blue where the line begins with a... (4 Replies)
Discussion started by: frustrated1
4 Replies

10. UNIX for Dummies Questions & Answers

replace a value with another in shell script

hai everybody, i am new to scripting,i have to replace a value with another value ina script...both the values i have got in two seperate variables...can anyone give me the syntax how to do this in a shell script... ie. e1=$5,e2=$6 v2=$1,v3=$7 now i want to... (2 Replies)
Discussion started by: Babu
2 Replies
Login or Register to Ask a Question