Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

irregardless

macrumors member
Original poster
Sep 6, 2006
38
0
all around you
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.
 

weldon

macrumors 6502a
May 22, 2004
642
0
Denver, CO
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).
 

irregardless

macrumors member
Original poster
Sep 6, 2006
38
0
all around you
weldon said:
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).

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).
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
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.
 

jsw

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
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
 

CanadaRAM

macrumors G5
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.
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
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 :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.