Anyway to write a script that write text into an existing text file

Discussion in 'Mac Programming' started by hypmatize, Jun 7, 2013.

  1. macrumors regular

    Joined:
    Feb 25, 2008
    Location:
    SoCal
    #1
    I'm a noob at scripting and don't really know where to start.

    What I am trying to do is write a script that when executed writes text into another text file in a specific location in the file. Is that possible? I know you can write to a text file but I'm not sure if you can put it in a specific location.


    If you can, I would really appreciate an help you guys can give me.
     
  2. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    I don't know much about scripting (I mostly write programs... I've written a very few Applescripts and Shell scripts...) but this sounds definitely doable.

    I suppose reading over the Apple Shell Scripting Primer (and doing whatever exercises it suggests) will get you going on your way:

    https://developer.apple.com/library/mac/documentation/OpenSource/Conceptual/ShellScripting/Introduction/Introduction.html
     
  3. macrumors 6502a

    Joined:
    Jun 17, 2003
    #3
    How do you want to specify the location? Line offset is easiest, but it can be done with byte offset.

    Do you expect the inserted text to overwrite what was there, or be inserted?

    Assume the file is:

    Code:
    aaa
    aaa
    aaa
    bbb
    bbb
    And you are inserting the text line "cccc" after the first 3 lines. Do you expect the output to be:

    Code:
    aaa
    aaa
    aaa
    cccc
    bbb
    bbb
    or:

    Code:
    aaa
    aaa
    aaa
    cccc
    bbb
    
     
  4. thread starter macrumors regular

    Joined:
    Feb 25, 2008
    Location:
    SoCal
    #4
    Thanks I'll take a look at it :D

    It would be the second example. I want it to do this.

    Code:
    write_preferences_at_exit true; # write_preferences_at_exit <bool>, Write preferences at exit #
    app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #
    
    to this

    Code:
    write_preferences_at_exit true; # write_preferences_at_exit <bool>, Write preferences at exit #
    mod "ETF.pack";
    mod "mod_grandcampaigngraphics.pack";
    mod "mod_BSM_4_1_Full_1_5.pack";
    mod "sponge.pack";
    mod "royalnavy2.pack";
    mod "NPIv0.3.0.pack";
    app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #
    
    it's basically a script to write into another script. I want it because that added text gets deleted everything I exit the application.

    Now that I read the script, couldn't I just change something in the script so it doesn't delete the added text instead of writing a script for script?

    Sorry if I wasn't clear in my OP. I guess I can just try and see what happens. :p
     
  5. macrumors 6502a

    Joined:
    Jun 17, 2003
    #5
    Change the config file to:

    write_preferences_at_exit false; # write_preferences_at_exit <bool>, Write preferences at exit #
    app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #

    It looks like the application already has functionality to avoid rewriting the file.
     
  6. hypmatize, Jun 8, 2013
    Last edited: Jun 8, 2013

    thread starter macrumors regular

    Joined:
    Feb 25, 2008
    Location:
    SoCal
    #6
    ok cool... That's what I assumed. My only programing experience was C# and CSS (if you CSS can be called programing :confused:), I should of known that :eek:.

    Edit:

    Tried it, but it still clears out the extra text after I exit : /
     
  7. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #7
    The way to think about this is to create NEW file. Say you want to insert data after line four.

    So the script does this:
    1) copy the first four lines to the new file
    2) copy new data to file.
    3) copy remainder of fist file to new file.

    Look at the shell command "split". At can cut a file into segments. Then you drop your new data into the list of segmants and then "cat" them all together.
     
  8. macrumors 6502a

    Joined:
    Jun 17, 2003
    #8
    Based on your example, that matches the first example I gave. That is, no overwrite of existing lines.

    But since your input file only has two lines and you are inserting text between the two lines the problem is greatly simplified.

    The following shell script is rough and assumes that the input file has two lines and you want the inserted text to go between them.

    Code:
    #!/bin/bash
    
    INPUT_FILE="input.txt"
    INSERT_FILE="insert.txt"
    OUTPUT_FILE="output.txt"
    
    input_head=`head -1 ${INPUT_FILE}`
    input_tail=`tail -1 ${INPUT_FILE}`
    
    echo ${input_head} > ${OUTPUT_FILE}
    cat ${INSERT_FILE} >> ${OUTPUT_FILE}
    echo ${input_tail} >> ${OUTPUT_FILE}
     
  9. thread starter macrumors regular

    Joined:
    Feb 25, 2008
    Location:
    SoCal
    #9

    Thanks! :D

    The file has 100's of lines in it though : /

    So do I count it out?
     
  10. macrumors member

    Joined:
    Jun 4, 2009
    #10
    If the file doesn't change too much then use diff and patch.
     
  11. macrumors newbie

    Joined:
    Jun 8, 2011
    #11
    You can use sed to append lines after a pattern:

    Code:
    #!/bin/bash
    
    match="write_preferences_at_exit true; # write_preferences_at_exit <bool>, Write preferences at exit #"
    
    insert=$(cat <<EOF
    mod "ETF.pack";\\
    mod "mod_grandcampaigngraphics.pack";\\
    mod "mod_BSM_4_1_Full_1_5.pack";\\
    mod "sponge.pack";\\
    mod "royalnavy2.pack";\\
    mod "NPIv0.3.0.pack";\\
    EOF)
    
    sed "/${match}/a\\
    ${insert}
    "
    
    This script will insert the text in the variable $insert after the line(s) matching the text in $match. The result will be written to standard output. To replace the original file (after making sure the script works as expected), redirect the output to a temporary file and then rename it:

    Code:
    $ script.sh < input.txt > tmp
    $ mv tmp input.txt
    
     

Share This Page