How to? Is there a way to delete a line in a file?

Jul 25, 2010
5
0
All of these are useful:
@FILECLOSE Close a file handle
@FILEOPEN Open a file handle
@FILEREAD Read next line from a file handle
@FILESEEKL Move a file handle pointer to a specified line
@FILEWRITE Write next line to a file handle
@FILEWRITEB Write data to a file handle
@TRUNCATE Truncate the file at the current position of the file handle pointer.
but none allow me to delete a line in a file?
 
May 20, 2008
9,426
63
Syracuse, NY, USA
is there something in TPIPE? Or maybe a CUT operation?
Sure, there's TPIPE's /grep. Use grep type 4 (extract non-matching lines). But you'll have to be able to describe the line you want not extracted uniquely with a regular expression. Below, I remove the "hamster" line.

Code:
v:\> type pets.txt
My dog is black.
My cat is grey.
My hamster is brown.
My bird is green.
My chameleon is green.
My fish have various colors.

v:\> tpipe /input=pets.txt /output=pets.new /grep=4,0,0,0,0,0,0,0,".*hamster.*"

v:\> type pets.new
My dog is black.
My cat is grey.
My bird is green.
My chameleon is green.
My fish have various colors.

v:\>
 
Jul 25, 2010
5
0
Thanks guys, although is more complex than what would be an equivalent "@FILEREMOVE" !

My need is to manage what is a file-based stack that is shared across many independent sessions and I want to "pop" the top value after it is read. If I logically invert the stack I guess I can use @TRUNCATE to achieve the result I want.
 
May 29, 2008
540
3
Groton, CT
If you just want to remove the first line of the file, try
Code:
TAIL /N+1 /N%@LINES[%sourcefile] %sourcefile > %tempfile & MOVE /Q %tempfile %sourcefile
 

samintz

Scott Mintz
May 20, 2008
1,336
12
Solon, OH, USA
if you know the max lines of your file, you can replace the %@lines[] with a number - e.g. 999999. Otherwise, the @lines function has to open the file, iterate over all the lines and count them up. Since you said the first line is the one you want to remove, I would use HEAD instead of TAIL. But in this usage they should behave the same.
Code:
HEAD /N+1 /N 999999 sourcefile
ALIAS RTRUNCATE=`HEAD /N+1 /N 999999 %1 > %1.tmp & MOVE /Q %1.tmp %1`
 
  • Like
Reactions: dcantor