Follow us on...
Follow us on Twitter Follow us on Facebook
Register
Results 1 to 3 of 3
  1. #1
    michaelsanford is offline Translator, Web Developer
    Join Date
    Oct 2002
    Location
    Ottawa/Montréal
    Posts
    2,280
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Question Output to an existing file, and replace strings with arguments?

    That subject line is a bit convoluted, I admit.

    What I'm trying to do is have an existing file act as a template, and then use a script to change strings in the file to other strings.

    I'm not up on my C, but I'm thinking of a printf-style output. The argument structure is probably wrong, but it's something like printf('I am %A years old', $age);

    I have a file full of %As and other formatting characters, and I want to replace the %As with a variable value, as a static file.

    Is this simple? I can find the instances easily enough with grep, but I don't know how to replace the string.

    Any help is appreciated, if you can decode this garbled explanation

  2. #2
    d1taylor is offline Writer, et. al.
    Join Date
    Aug 2002
    Location
    Boulder, CO
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here's how I would approach this: a two part process, where the first process parses your data files, inserting variables names into each field:

    ( while read field1 field2 field3 field4 ; do
    echo "f1='$field1'; f2='$field2'; f3='$field3'"
    ) < input-data-file > tempfile

    then in a second loop, use the tempfile as input, feed it to the eval command, then simply substitute all occurances of, say, '$f1' with the actual value of the variable f1:

    ( while read inputline ; do
    eval $inputline
    sed "s/\$f1/$f1/g;s/\$f2/$f2/g;s/\$f3/$f3/g" < templatefile
    ) < tempfile

    I hope that makes sense. It might be slightly off with syntax since this is top of my head, but as long as your datafile is well formed (e.g., there's a common delimiter between fields (if it's not a space, change the IFS value to use that instead, btw).

    Cheers and good luck!
    Author, Learning Unix for Mac OS X (O'Reilly), among other books.
    Check out http://books.intuitive.com/

  3. #3
    michaelsanford is offline Translator, Web Developer
    Join Date
    Oct 2002
    Location
    Ottawa/Montréal
    Posts
    2,280
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Cool thanks I'll give it a shot and let you know who it turns out!

 

 

Similar Threads

  1. How to compile stuff
    By fintler in forum Unix & X11
    Replies: 1
    Last Post: August 28th, 2002, 07:57 AM
  2. How do I prevent OSX asking me if I want to replace a file EVERY TIME?
    By migato in forum Mac OS X System & Mac Software
    Replies: 1
    Last Post: May 27th, 2002, 09:43 PM
  3. HOW TO: Install BIND DNS 9.2 with RNDC Controls
    By Dymas in forum Mac OS X System & Mac Software
    Replies: 6
    Last Post: March 16th, 2002, 09:12 PM
  4. UNIX related things... (tr, cut, awk, and permissions)
    By simX in forum Apple News, Rumors & Discussion
    Replies: 5
    Last Post: December 19th, 2001, 02:33 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •