0

Let say I have the following text file :

  1. DEMO
  2. TEXT1
  3. TEXT2
  4. DEMO
  5. etc....

  6. DEMO

  7. TEXT1
  8. TEXT2
  9. DEMO
  10. etc....

  11. DEMO

  12. TEXT1
  13. TEXT2
  14. DEMO
  15. etc....

I would like to added some text between the first TEXT1 and TEXT2 (line 2/3)

so the text would be like :

  1. DEMO
  2. TEXT1 MY TEXT HERE
  3. TEXT2
  4. DEMO
  5. etc....

  6. DEMO

  7. TEXT1
  8. TEXT2
  9. DEMO
  10. etc....

  11. DEMO

  12. TEXT1
  13. TEXT2
  14. DEMO
  15. etc....

I know how I can filter the text to get the value of any text that would be there with the code bellow, but how can I add some text on my text file at this empacement ?

property leftEdge1 : "2. TEXT1"
property rightEdge1 : "3. TEXT2"
set myNewCaseNote to ""
set newFile to (path to desktop folder as text) & "_Note_Backup.txt"
set theSource to read file newFile
set theText to Unicode text
try
    set saveTID to text item delimiters
    set text item delimiters to leftEdge1
    set classValue to text item 2 of theSource
    set text item delimiters to rightEdge1
    set myCaseNote to text item 1 of classValue
    set text item delimiters to saveTID
    myCaseNote
end try
1

Text processing should be done with shell script. Usually it can be done in one short line:

sed 's;^[^0-9]*2 TEXT1;& MY TEXT HERE;' text_file.txt

Assuming there wasn't some sort of indentation problem above. Based on what it sounds like you meant though, here's another solution:

sed 's;^[^0-9]*2 TEXT1;&\'$'\n'' MY TEXT HERE;' text_file.txt

AppleScript is not the right tool for this job.

1

As the previous answer explained, there are better ways to do what you want, but if you want to do it using AppleScript, you could accomplish it with a modified version of this:

on SearchAndReplace(SearchString, ReplaceString, SearchIn)
    set text item delimiters to SearchString
    set temp to text items of SearchIn
    set text item delimiters to ReplaceString
    set z to temp as string
    set text item delimiters to ""
    return z
end SearchAndReplace

on run
    set OriginalFullText to "DEMO
TEXT1
TEXT2
DEMO
etc....
DEMO
TEXT1
TEXT2
DEMO
etc....
DEMO
TEXT1
TEXT2
DEMO
etc...."

    set test to "TEXT1
TEXT2"
    set Text1 to "TEXT1"
    set Text2 to "TEXT2"
    set MyText to Text1 & " My Text Here" & return & Text2
    set Text2 to my SearchAndReplace(test, MyText, OriginalFullText)
end run

I hope it helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .