Previous topicNext topic

Text module expression: Show only last line of text-file

Questions, comments, feedback, etc.
Post Reply
ppirx
Posts: 3
Joined: Wed Jan 31, 2024 9:56 am

Text module expression: Show only last line of text-file

Post by ppirx »

Plz help, greenhorn here!
Someone knows how to screen only the last line of a text-file? -is there anyone who can help me with an expression please!

Thank you!
Sadler
Posts: 1143
Joined: Sat Aug 02, 2014 7:10 pm
Location: London, UK

Re: Text module expression: Show only last line of text-file

Post by Sadler »

There's no expression or internal that will give you the last line of a text file. At the risk of being obvious, why not just enter the number of lines?
ppirx
Posts: 3
Joined: Wed Jan 31, 2024 9:56 am

Re: Text module expression: Show only last line of text-file

Post by ppirx »

..I have to read from an automatic generated and permanently growing textfile wherein only always the last line is interesting to me.

The expression only needs to count the lines of my textfile and return that number..
The C++ Mathematical Expression Toolkit Library (ExprTk) is used by Magic, right? According to the ExprTk-website it has following capabilities:
"File-IO package (routines include: open, close, read, write, getline, support for binary and text modes"
-so, shouldn´t it be possible?

Thank you!
Sadler
Posts: 1143
Joined: Sat Aug 02, 2014 7:10 pm
Location: London, UK

Re: Text module expression: Show only last line of text-file

Post by Sadler »

Magic only uses the maths and logic parts of the expression toolkit. Expressions must return a number.

It is not possible to write your file in reverse? Or find some way to tail it outside of Magic.
ppirx
Posts: 3
Joined: Wed Jan 31, 2024 9:56 am

Re: Text module expression: Show only last line of text-file

Post by ppirx »

..I understand.
Thank you for sharing your knowledge!
I already solved the problem with Applescript, and as you suggested, outside of Magic.

I wrote an applescript that searches the last line and writes it into a new txt-file, every 5 seconds until I stop the script.. voila!

Code: Select all

# Start Repeat
set theCount to 10

repeat
	set theCount to theCount + 1
	
	# Determine input and output file paths.
	# Note: Use POSIX-format paths ('/' as the separator).
	set inFile to "PLAYLIST.txt"
	set outFile to "LINE.txt"
	
	# Use a shell command to extract the last line from the input file using `sed`
	# and write it to the output file.
	do shell script "sed -n '$ s/.*: \\(.*\\)/\\1/p' " & quoted form of inFile & ¬
		" > " & quoted form of outFile
	
	# Delay (Seconds)
	delay 5
	
	# End Repeat
end repeat

return theCount

return input
Post Reply