Trim last N lines from a file

May 30, 2008
235
2
I have a text file type where I have a need to discard the first N1 lines and the last N2 lines but keep everything in the middle. The length of the middle will usually vary from file to file.

The first N1 lines can be removed by using TAIL to skip them and then use a large N value to keep the rest.

I haven't found an easy way to remove the last N2 lines though. It seems to me that a combination of HEAD and TAIL should allow that to be done.

At least that is the case for the GNU versions.
http://stackoverflow.com/questions/...-from-the-tail-of-a-file-without-the-use-of-h

Might there be another way to achieve the same in TCC?
 
May 20, 2008
3,515
4
Elkridge, MD, USA
nikbackm wrote:
| I have a text file type where I have a need to discard the first N1
| lines and the last N2 lines but keep everything in the middle. The
| length of the middle will usually vary from file to file.
|
| The first N1 lines can be removed by using TAIL to skip them and
| then use a large N value to keep the rest.
|
| I haven't found an easy way to remove the last N2 lines though. It
| seems to me that a combination of HEAD and TAIL should allow that to
| be done.
|
| Might there be another way to achieve the same in TCC?

Certainly there is. Use the @LINES[] function to determine the file size,
and a little arithmetic to determine the number of lines to retain K. Now
the command
TAIL /n+N1 /nK file
will do exactly what you need. Just remember that the value of @LINES is an
offset, not line count; an empty file returns -1, a 1-line file returns 0.
The result will be something like this (NOT TESTED):

TAIL /n+N1 /n%@eval[ %@lines[file] - N1 - N2 + 1 ]
--
HTH, Steve
 
May 20, 2008
11,285
95
Syracuse, NY, USA
On Wed, 07 Oct 2009 09:01:50 -0500, Steve Fábián <> wrote:

|Certainly there is. Use the @LINES[] function to determine the file size,
|and a little arithmetic to determine the number of lines to retain K. Now
|the command

I'm a little surprised that's necessary (as it seems to be). I suppose TAIL
knows how many lines are in the file (and has used up some time figuring it
out). The user shouldn't have to do it again. If HEAD has an option to skip
the first few lines of a file, TAIL should have one to skip the last few.
--
- Vince
 
May 20, 2008
3,515
4
Elkridge, MD, USA
vefatica wrote:
| I'm a little surprised that's necessary (as it seems to be). I
| suppose TAIL knows how many lines are in the file (and has used up
| some time figuring it out). The user shouldn't have to do it again.
| If HEAD has an option to skip the first few lines of a file, TAIL
| should have one to skip the last few. --

I agree. I suggest that the HEAD, TAIL, and TYPE commands could be collapsed
into a single enhanced TYPE command, which could have /B(egin), /E(nd), and
/Nn options. /B and /E would specify the beginning and ending line number to
be displayed, and /N the number of lines to be displayed, except when /X is
also used, they mean be bytes. Both /B and /E would work in the fashion used
by functions parsing lines, i.e., positive numbers start from the beginning
of the file, negative numbers count backwards from the end of file. In case
of overspecification (all three of the options specified) the command would
display only lines that match all restrictions. The parser could easily
convert - invisibly to the user - HEAD and TAIL commands, with their current
syntax, to the new TYPE command.

Another suggestion: the above partial file selection mechanism for the FFIND
command.
--
Steve
 

samintz

Scott Mintz
May 20, 2008
1,488
15
Solon, OH, USA
Wouldn't it be easier to just modify the HEAD command to accept negative
numbers so it works more like the GNU command?

-Scott

Steve F$BaC(Bi$BaO(B <> wrote on 10/07/2009 11:35:07 AM:


> vefatica wrote:
> | I'm a little surprised that's necessary (as it seems to be). I
> | suppose TAIL knows how many lines are in the file (and has used up
> | some time figuring it out). The user shouldn't have to do it again.
> | If HEAD has an option to skip the first few lines of a file, TAIL
> | should have one to skip the last few. --
>
> I agree. I suggest that the HEAD, TAIL, and TYPE commands could be
collapsed

> into a single enhanced TYPE command, which could have /B(egin), /E(nd),
and

> /Nn options. /B and /E would specify the beginning and ending line
number to

> be displayed, and /N the number of lines to be displayed, except when /X
is

> also used, they mean be bytes. Both /B and /E would work in the fashion
used

> by functions parsing lines, i.e., positive numbers start from the
beginning

> of the file, negative numbers count backwards from the end of file. In
case

> of overspecification (all three of the options specified) the command
would

> display only lines that match all restrictions. The parser could easily
> convert - invisibly to the user - HEAD and TAIL commands, with their
current

> syntax, to the new TYPE command.
>
> Another suggestion: the above partial file selection mechanism for the
FFIND

> command.
> --
> Steve
>
>
>
>
 

rconn

Administrator
Staff member
May 14, 2008
12,315
148
> I'm a little surprised that's necessary (as it seems to be). I suppose
> TAIL knows how many lines are in the file (and has used up some time
> figuring it out). The user shouldn't have to do it again.

TAIL has no idea how many lines are in the file. (It starts from the end
and counts backwards.)

Nor would you want it to -- it would be *seriously* slow on large files.

Rex Conn
JP Software
 
May 30, 2008
235
2
Certainly there is. Use the @LINES[] function to determine the file size,
and a little arithmetic to determine the number of lines to retain K. Now
the command
TAIL /n+N1 /nK file
will do exactly what you need. Just remember that the value of @LINES is an
offset, not line count; an empty file returns -1, a 1-line file returns 0.
The result will be something like this (NOT TESTED):

TAIL /n+N1 /n%@eval[ %@lines[file] - N1 - N2 + 1 ]
--
HTH, Steve

Thanks, this worked like a charm!

I had an idea to scan the file with a DO loop and find the marker that begins the N2 part but this is much easier.
 
Similar threads
Thread starter Title Forum Replies Date
C Last TCMD that supports win 7? Support 5
M No line wraps if last line of the screen buffer is reached Support 5
R "The Specified Account Already Exists" - Can't Reinstall To Fix Protection Screw-Up of Last Week Support 8
P No linefeed on last line of console window Support 2
B How can a user know the last 'true command' executed Support 1
B Fixed FFIND doesn't find last character in file without CR/LF at EOF Support 1
rps Fixed Using last argument variable ! Support 5
B %@lines doesn't count last line without cr/lf at eof Support 1
J WAD First Record, Last Record, Next Record, Previous Record Support 11
greyfairer Very slow startup since last Windows 7 Update (14.03.59 x64) Support 9
fpefpe Last 13 Support 9
dcantor Fixed Ctrl+Shift+Insert repeats last character Support 3
A How to? Remove last argument on command line Support 4
Stefano Piccardi did the forum software change in the last two days? Support 2
R TC11 - copy last cmd to bottom of history Support 4
rconn Last call for v12 feature requests Support 59
vefatica Continued lines (^) in a library routine? Support 5
R Lines between COMMENT & ENDCOMMENT not ignored Support 7
A TCMD - Dir Command puts out blank lines? Support 16
T FFind - can we display n number of lines after the find? Support 2
T WAD Difference between TCC and CMD in handling multi-command lines Support 6
JohnQSmith Weird DIR output (missing lines) Support 1
fpefpe Documentation Comment lines? Support 2
nickles Fixed Executing long command lines in a batch generates file name errors Support 16
nikbackm How to? Find duplicate lines in text file Support 0
S Documentation HELP nit: @LINES Support 5
B Select and run lines from Command Input panel? Support 1
D WAD New envars for @LINES are not hidden Support 8
T Documentation @LINE[CON,n] @LINES[CON] not documented (bug?) Support 4
M How to? Parsing lines containing double quotes... Support 3
Stefano Piccardi echoerr truncates long lines Support 9
M Just an out-of-curiosity question the List command vs. the @Lines function... Support 13
samintz How to? Pull out multiple matching lines Support 5
W Copy+Paste without breaking long lines as default? Support 1
L "for" in multiple lines Support 2
vefatica @LINES, long lines, TCC disappears Support 7
D Blank lines in jpstree.idx Support 3
T @Word from Lines with Pipes Support 3
vefatica @LINES[]? Support 1
J Extra blank lines during paste Support 6
vefatica How many lines are in this file? Support 17
T How do you echo lines with special characters to file? Support 4
B @lines: first line in the file is numbered 1 Support 0
R Reading an Unicode file with more than 8191 lines Support 1

Similar threads