Purchase Solution

Shell scripting

Not what you're looking for?

Ask Custom Question

How would I write a shell script, change-lines, which will substitute a string for a replacement string for each occurance of the string in files
specified? The original file will be saved, with the same filename
with the .keep extension unless the -n option is present. I don't mind using temporary file for this script. The search string and/or the
replacement string can have special characters in them.
The script will use

* the option -s "string" for the search string
* the option -r "string" for the replacement string
* the -n option to prevent a backup file from being created
* the -h option to print a usage statement and exit

The script will exit with an error status and print an error message:

* if the -s option is not present.
* if the -r option is not present.
* if the string is not specified with the -s option
* if the string is not specified with the -r option
* if there are any other options specified
* if there are no input files specified
* if the input file cannot be read by the current process
* if the input file cannot be written by the current process
* if you cannot create the backup file.

Here is the usage statement for the script:

change-lines [-n] -s search string -r replace string files ...

-n do not backup the original file
-s search string the search for this string
-r replace string replace the search string with
this string
-h print this message

Examples:

prompt> change-lines -s foobar -r FooBar *.c
This script will edit all the files *.c, and each occurrence
of foobar it will be replaced by FooBar it will be replaced by
FooBar. Each file will be backed up.

prompt> change-lines -s "SHELL IS NOT FUN" -r "SHELL PROGRAMING IS A LOT OF FUN" /tmp/file1.txt
This script will edit all the file /tmp/file1.txt , and each
occurrence of SHELL IS NOT FUN will replaced by the text, SHELL
PROGRAMMING IS A LOT OF FUN. A backup file /tmp/file1.txt.keep
will be created.

Purchase this Solution

Solution Summary

Shell scripting is summarized.

Purchase this Solution


Free BrainMass Quizzes
Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Basic Networking Questions

This quiz consists of some basic networking questions.

Javscript Basics

Quiz on basics of javascript programming language.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.