Terminal - Parsing data by paragraphs

Hi All,
I am working on a script that would take a text file and copy the data of a paragraph and send it to a text variable in Applescript.

For example, the text file would be named events.txt and looks something like this below:

Both of these would be separate events and I was trying to use AWK to create a separation between the two and then loop through the events and send them to a data variable. My present script would be something like this:


set theOutput to paragraphs of (do shell script "cat events.txt ")
set n to 1
repeat while n ≤ (count theOutput)
	
	set thisItem to item n of theOutput
	set thisItemPath to (thisItem's POSIX path's text)'s quoted form
	
	log thisItem
	log thisItemPath
--do something here

end repeat

I am sorry if this is vague or difficult to understand. Any help would be appreciated.

I don’t understand the question but I guess you were looking for a proper way to parse your file…


set status to 0
set allEvents to {}
set currentEvent to missing value
repeat with thisLine in paragraphs of (do shell script "cat events.txt ")
	if length of thisLine = 0 then
		set currentEvent to missing value --to reset incomplete fills
		set status to 0
	else if status = 0 then
		set currentEvent to {theDescription:contents of thisLine, theDate:"", theLocation:""}
		set status to 1
	else if status = 1 then
		set theDate of currentEvent to contents of thisLine
		set status to 2
	else if status = 2 then
		set theLocation of currentEvent to contents of thisLine
		set end of allEvents to currentEvent
		set status to 3 --with a non existing status the loop goes on till the end of the file or till the next empty line
	end if
end repeat
return allEvents

EDIT:

Maybe I should clarify why I use this method. I used this way of parsing simply because it doesn’t matter if a file ends with an empty line or how many empty lines there are in a file. Nor does it matter if there incomplete events (like now already the ‘-------------’), if an event is not completely filled it will be ignored. and at last if there is an event with more than 3 items it doesn’t matter as well. it takes only the first 3 lines of the event. Of course there is a drawback because an empty description, date or location would be considered as an incomplete fill and will not be readed as an event. but you also didn’t mention that :).

Hello there,
I am going to run through this and see if I understand this and get back to you. Thank you so much for your time and expertise.

Thank you for the reply. I sort of get it, except for how it is reading the description. Here is my Applescript (well your applescript with my file injected):


set status to 0
set allEvents to {}
set currentEvent to missing value
repeat with thisLine in paragraphs of (do shell script "/Users/boyd/Desktop/test.txt")
	if length of thisLine = 0 then
		set currentEvent to missing value --to reset incomplete fills
		set status to 0
	else if status = 0 then
		set currentEvent to {theDescription:contents of thisLine, theDate:"", theLocation:""}
		set status to 1
	else if status = 1 then
		set theDate of currentEvent to contents of thisLine
		set status to 2
	else if status = 2 then
		set theLocation of currentEvent to contents of thisLine
		set end of allEvents to currentEvent
		set status to 3 --with a non existing status the loop goes on till the end of the file or till the next empty line
	end if
end repeat
return allEvents

My text file looks like this:

I am getting an error on output that I am not understanding enough to resolve the issue.

Thank you so much!

the cat command is missing in the shell script string.
But AppleScript could read a plain text file directly


set plainText to read file ((path to desktop as text) & "test.txt")
repeat with thisLine in paragraphs of plainText

HI Guys,
That is great! It makes list of objects:

Now, I am trying to get the first list as a text parameter that looks like this:

Basically, I have a graphics card that can output text realtime, and so, I repeat through my list of currentEvents to pull each event up for 10 seconds and then go away, grab the text for the next event, send it to the output, and so on. I know I am having trouble articulating this, but I will show paste the code for the rest of it in order to clarify.


--this was the original SH, but it only took one line at a time.  I needed 3 lines at a time.
set theOutput to paragraphs of (do shell script "cat /Users/boyd/Desktop/TestFile1.txt")
set n to 1
repeat while n ≤ (count theOutput)
	
	set thisItem to item n of theOutput
	set thisItemPath to (thisItem's POSIX path's text)'s quoted form
	
	log thisItem
	log thisItemPath
	
	
	set n to n + 1
	
	set Parameter to thisItem
	tell application "OnTheAir CG Controller"
		
		-- TEXT must be put inside the CG project with a % before and after: %My_Text%, it will be replaced by the parameter set in OnTheAir Video
		send command "*SCRIPT*TEXT='" & Parameter & "'"
		send command "*PROJECT_PLAY*/Volumes/HECTVLibrary/CG/Projects/PresentsLT.cg"
		delay 9
		send command "*PROJECT_STOP*/Volumes/HECTVLibrary/CG/Projects/PresentsLT.cg"
		
	end tell
	
	
	
end repeat