Computer problems,Computer help
*AX SOFT>>>Programming & Design

BASH - How To (Replace specific text within a file)?


I want to take a template file; which will later be used for an application configuration file and have certain lines within that configuration have a tag that I can later replace with any value when using a script. Example:
(template file)
Max Hold Time: %time%
Path: %path%

-the script runs and would replace those values in that template file with my own values
%time% replaced with 14400
%path% replaced with /somepath/somedirectory

Any ideas?

Thanks...

I know bash itself might not have all the answers, however, any perl or other external applications that you know would do this - an example using that code to do the job would be greatly appreciated. Thanks

#!/bin/bash

time=14400
path=/somepath/somedirectory
sed -e "s!%time%!$time!; s!%path%!$path!" template.txt > file.txt

Bash doesn't have sufficient text manipulation abilities built-in to achieve this; it's a shell after all. You could use bash to execute other programs which could perform these operations, such as sed, or perl.

If you have Perl, I would recommend you use that, and install the Template Toolkit (TT2). http://www.template-toolkit.org/

There are plenty of examples, and TT2 comes with some handy utilities for performing bulk template operations.

I use TT2 for producing HTML, XML, e-mail, CSV, and occasionally TeX. It works well.

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • Best practice looping through database columns or hardcode columnnames?

    Looping through a database by far is better. Because then if you want to expand you already have an object ready to handle it. Dynamic is the way to go.

    ...
  • Order online ! help.?

    You need a FormMail script. You just insert your e-mail address and the name of the Order Form File that you will design to accept details of your customer's order, and you will receive an e-...

  • How to terminate a Java while loop using a string?

    Your while condition will always evaluate to true. The expression str != "done" asks whether the reference str is different from the reference "done" - that is, do these ...

  • How do you automatically load a javascipt?

    Put the javascript in a file and in a subfolder of your project and then reference it in the header Example: <script language="javascript" src="scripts/general.js"><...

  • Is there any basic principles or codes needed to design a web page?

    Checkout AceHtmlPro (Free Edition) which will help you learn html and allows for easy page generation. Great for anyone starting out.

    ...
  • Dreamweaver MX: Why has the background & links disappeard on my 3rd page when I link to it from my homepage?

    Where exactly are you storing the specification of your links? In each page? Or in a separate style sheet (CSS)? If you're not using an external style sheet, I highly recommend starting to do ...

  • Unix help!!?

    Files in Unix filesystems use inodes as structures that hold the actual file data on disks. inode number uniquely identifies a physical file on disk. Hard links are pretty much multiple names fo...

  • Entering words in to C++?

    It's as simple as this, in C++: string s; cin >> s;

    ...
  •  

    Categories--Copyright/IP Policy--Contact Webmaster