AppleScript added value inside text

applescripttext;

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

Best Answer

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.