Removing lines not ending or starting with specific text

Please help! Is there a script that remove lines not ending or starting with specific text? I can’t find anything.

This is what I’m trying to do. I want to remove lines that either don’t start with “20210203” nor ending with “zip”.

There are a number of approaches that can be used and the following is one:

set theText to "20210203_Newsprint_207008_13676.zip
extras
20210127_Newsprint_202839_10550.zip
20210203_Corner_Store_205666_10550.zip
job #59083
20210203_Newsprint_206487_11010.zip
20210130_NewYork_206333_10492.zip
New York is cold this time of year.
20210203_Newsprint_206487_11010"

set beginText to "20210203"
set endText to "zip"

set newText to {}
repeat with aParagraph in (paragraphs of theText)
	set aParagraph to contents of aParagraph
	if aParagraph begins with beginText or aParagraph ends with endText then
		set end of newText to aParagraph
	end if
end repeat

set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set newText to newText as text
set AppleScript's text item delimiters to ATID

newText

The above gives me the opposite of what I want. I want the end result to be all lines that either starts with “20210203” or ends with “zip”.

Sorry about that. I’ve modified my script in Post 2.

Every paragraph of your text that begins with 20210203 also ends with zip. So, I’ve added a paragraph to the text just to demonstrate that a line that begins with 20210203 but does not end with zip is returned, as follows:

thank you! you’re awesome!

4bitm. I’m glad that worked and thanks for the kind words.

My ASObjC topic of study today was filters and I decided to use 4bitm’s request as a working example. This was fairly straightforward until I decided to use tokens, which took the better part of an hour to resolve. I compared this script with that in Post 2 above and the script in Post 2 was marginally faster.

use framework "Foundation"
use scripting additions

set theText to "20210203_Newsprint_207008_13676.zip
extras
20210127_Newsprint_202839_10550.Zip
20210203_Corner_Store_205666_10550.zip
job #59083
20210203_Newsprint_206487_11010.zip
20210130_NewYork_206333_10492.zip
New York is cold this time of year.
20210203_Newsprint_206487_11010"

set beginText to "20210203"
set endText to "zip"

set theList to paragraphs of theText
set theArray to current application's NSArray's arrayWithArray:theList
set thePred to current application's NSPredicate's predicateWithFormat_("(self BEGINSWITH[c] %@ OR self ENDSWITH[c] %@)", beginText, endText)
set theFilteredArray to ((theArray's filteredArrayUsingPredicate:thePred) as list)

set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set newText to theFilteredArray as text
set AppleScript's text item delimiters to ATID

newText

I modified the beginText to a prompt.

Hi 4bitm.

Just to note that:

  1. The posting tags for AppleScript code on this site are [applescript] and [/applescript]. They cause the code to be displayed with a clickable link which opens it in people’s default script editor. There’s a button for them above the text field on posting pages.

  2. TextWrangler itself was (and BBEdit still is) powerfully scriptable, although much of it isn’t exactly intuitive:

set beginText to "20210203"
set endText to "zip"
-- Be careful with this. Any characters in the above strings which are also grep metacharacters will need to be escaped.
set grepPattern to "^(?!" & beginText & ")|(?<!" & endText & ")$"

tell application "TextWrangler"
	tell front document
		process lines containing matching string grepPattern output options {deleting matched lines:true} with matching with grep without case sensitive
	end tell
end tell