Inserting characters after X number of line breaks in a text file?

Discussion in 'Mac Programming' started by irregardless, Oct 22, 2006.

  1. irregardless macrumors member

    Joined:
    Sep 6, 2006
    Location:
    all around you
    #1
    This seems like it should be simple, but I can't find any tools that will do it and my attempts at scripting it have failed. Sherlock Google has not been able to assist either. :(

    Here's the deal:

    I have a long text file with multiple line breaks. I need to insert more text at the end of every X number of lines (probably 5 or 6 depending on how it changes the flow). The new text is the same in all cases.

    I've got Text Wrangler and tried using its grep function. I managed to accomplish this task at the end of every line (which I could have done with a standard find-and-replace).

    Basically what I need is this:

    line 1.
    line 2.
    line 3. new text
    line 4.
    line 5.
    line 6. next text.

    Is there a way to do this? and how complicated is it?

    Thanks All.
     
  2. weldon macrumors 6502a

    weldon

    Joined:
    May 22, 2004
    Location:
    Denver, CO
    #2
    How about simple "find and replace" in MS Word or any other app with that feature. Just "find" 6 line breaks and then replace all the line breaks at once with your new text (including line breaks).
     
  3. irregardless thread starter macrumors member

    Joined:
    Sep 6, 2006
    Location:
    all around you
    #3
    If I had 6 sequential line breaks, that would work fine. But my lines all have different text on them. I'm sorry if I wasn't clear.

    My text file is like this:

    line 1: The quick brown fox. <break>
    line 2: Henry went out. <break>
    line 3: Red cars are neat. Let's get one. <break>

    etcetera

    In this case, I'd like to add the new text to the end of every third line (or so).
     
  4. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #4
    You should check into awk since it's a programmable command line utility. While it's not simple to use, you could probably find an awk script that you could adapt and learn something in the process.
     
  5. jsw Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #5
    Download http://formymac.com/MR/LineAdder.class

    Quick question: do you want the text added to the end of the nth line, or added as a new line every n lines?

    If it's the former, open Terminal, go to the directory you saved the file, and type:
    Code:
    java LineAdder fileToUse fileToWrite lineSpacing "text to use"
    for example
    Code:
    java LineAdder old.txt new.txt 3 "more text to add"
    to turn:

    line1
    line2
    line3
    line4
    line5
    line6

    into:

    line1
    line2
    line3more text to add
    line4
    line5
    line6more text to add


    If it's the latter, open Terminal, go to the directory you saved the file, and type:
    Code:
    java LineAdder fileToUse fileToWrite lineSpacing "text to use" newline
    for example
    Code:
    java LineAdder old.txt new.txt 3 "more text to add" newline
    to turn:

    line1
    line2
    line3
    line4
    line5
    line6

    into:

    line1
    line2
    line3
    more text to add
    line4
    line5
    line6
    more text to add
     
  6. CanadaRAM macrumors G5

    CanadaRAM

    Joined:
    Oct 11, 2004
    Location:
    On the Left Coast - Victoria BC Canada
    #6
    Excel will do it as long as none of the lines are longer than 255 characters.

    Import to Excel. Each line will be on a new row. in another column Run a series to count lines.
    Calculation to determine if a line is the nth line
    Concatenation calculation to add a specific unique character (like ~) to the end of nth lines only
    Export the concatenated text, open in word processor. Replace ~ with <break>{additional text}

    If you want the added text to be variable, that just means altering the concatenation calculation to add ~{calculated text string} instead of just ~
    Then in the word processor you replace ~ with <break>

    Excel is often overlooked as a tool for sorting text, creating calculated or concatenated text out of a variety of sources, and prepping data for input to databases.
     
  7. irregardless thread starter macrumors member

    Joined:
    Sep 6, 2006
    Location:
    all around you
    #7
    Beautiful! This worked perfectly. Big High-5! Thank you and to everyone who offered some help and fast.

    You guys are the reason this place is so great.

    Cheers!
     
  8. mufflon macrumors 6502

    Joined:
    Sep 15, 2006
    #8
    well if you need to make

    line text1
    line text2
    line text3

    into

    line
    line
    line
    line text1
    ...

    then just pm me - I got some java scripts I should easily be able to retrofit, no work load @ uni atm and I'm bored :)
     

Share This Page