removing all paragraphs between two matches

Hello, first of all apologies for asking this probably for the thousandth time but believe it or not I can’t find a solution by searching, at least not one which is close enough to what I want to achieve.
I’m trying to remove html/xml code from a text file and leave the last lines in the document, the block of code I want to get rid can be different lengths so remove by line number is not an option.
I have achieved this before using Textwrangler, some applescript and automator but my manager wants me to remove 3rd party software from the script as much as possible.
I’m not very concerned about getting the name: title: etc. deleted as well in this script as I can do that myself but if you know of some nice way of doing both things…
Thanks for your help

this is how the txt file looks:
xxxxx
xxxx
xxx
xxxxx
xxxxxx
(200 lines ±)
name: useful line 1
position: useful line 2
gender: useful line 3

I’m not understand clearly.

Are you wanting to remove everything before “name:”
or are you wanting to remove line 6 thru the last line before “name:” ?

set theText to "xxxxx
xxxx
xxx
xxxxx
xxxxxx
(200 lines +-)
name: useful line 1
position: useful line 2
gender: useful line 3"


set theKey to "name:"
# case #1
set lastLines to items 2 thru -1 of my decoupe(theText, theKey)
set shortenText to theKey & my recolle(lastLines, theKey)
(*
"name: useful line 1
position: useful line 2
gender: useful line 3"
*)
# case2
set theBeg to my recolle(paragraphs 1 thru 5 of theText, linefeed)
set lastLines to items 2 thru -1 of my decoupe(theText, theKey)
set shortenText to theBeg & linefeed & theKey & my recolle(lastLines, theKey)
(*
"xxxxx
xxxx
xxx
xxxxx
xxxxxx
name: useful line 1
position: useful line 2
gender: useful line 3"
*)

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) dimanche 30 octobre 2016 22:04:07

Thanks Yvan! I spent all weekend trying to achieve that!
I’m stuck again however… I am trying to substitute this (from your script above):

set theText to “xxxxx
xxxx
xxx
xxxxx
xxxxxx
(200 lines ±)
name: useful line 1
position: useful line 2
gender: useful line 3”

with this:
set theText to allText
(The allText variable is passed from automator with all the text of a file selected, before this I tried choosing the file, file path, nothing works!)

The script below is my last attempt to pass the contents of a txt file.


set theText to allText

set theKey to "name:"
# case #1
set lastLines to items 2 thru -1 of my decoupe(theText, theKey)
set shortenText to theKey & my recolle(lastLines, theKey)

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Thanks for your help!

You may use :

set theFile to choose file with prompt "Choose a text file:" of type {"txt"}

set theText to read theFile

set theKey to "name:"
# case #1
set lastLines to items 2 thru -1 of my decoupe(theText, theKey)
set shortenText to theKey & my recolle(lastLines, theKey)

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

You may use this alternate version using ASObjC features.

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set thePath to POSIX path of (choose file with prompt "Choose a text file:" of type {"txt"})
set theText to its readFileAt:thePath

set theKey to "name:"
# case #1
set lastLines to items 2 thru -1 of (my split:theText usingString:theKey)
set shortenText to theKey & (my concatList:lastLines usingString:theKey)

#=====

on readFileAt:thePath
	set theText to current application's NSString's stringWithContentsOfFile:thePath encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
	return theText as text
end readFileAt:

#=====

on split:sourceString usingString:d1
	set sourceString to current application's NSString's stringWithString:sourceString
	return (sourceString's componentsSeparatedByString:d1) as list
end split:usingString:

#=====

on concatList:theList usingString:d1
	set anArray to current application's NSArray's arrayWithArray:theList
	return (anArray's componentsJoinedByString:d1) as text
end concatList:usingString:

#=====

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) lundi 31 octobre 2016 11:45:23