A very rough awk implementation:
BEGIN{
OLDFILENAME="";
}
FNR==1{
if (OLDFILENAME != "") {
printf("#### Processed (chars: %s - lines: %s)\n", FWC, FLC);
}
printf("#### Processing: %s\n", FILENAME);
OLDFILENAME=FILENAME;
FWC=0;
FLC=0;
}
{
printf("%04d - %s\n", FNR, $0);
FWC = FWC + length($0);
FLC = FLC + 1;
}
END{
if (OLDFILENAME != "") {
printf("#### Processed (chars: %s - lines: %s)\n", FWC, FLC);
}
}
Execute awk -f AWKFILE trapetz simpson
to get:
#### Processing: trapetz
0001 - x = 0:0.0001:7pi
0002 - plot(x, sin(x).*cos(x))
0003 - Area = trapz(x, sin(x).*cos(x))
#### Processed (chars: 70 - lines: 3)
#### Processing: simpson
0001 - f = inline(sin(x).*cos(x));
0002 - Area2 = quad(f, 0, 7pi, 1e-16)
#### Processed (chars: 57 - lines: 2)
This is actually a function of the terminal emulator you are using (xterm, gnome-terminal, konsole, screen). An alternate screen, or altscreen, gets launched when programs such as less
or vim
are invoked. This altscreen has no history buffer and exits immediately when you quit the program, switching back to the original screen which restores the previous window content history and placement.
You can prevent less
from launch in an altscreen by passing the argument "-X".
less -X /path/to/some/file
You can also pass "-X" as an environment variable. So if you are using bash
, place this in ~/.bashrc
:
export LESS="-X"
However, this disbles the termcap (terminal capability) initialization and deinitialization, so other views when you use less
may appear off.
Another option would be to use screen
and set the option altscreen off
in your ~/.screenrc
. less
will not clear the screen and should preserve color formatting. Presumably tmux
will have the same option.
This blog entry describes the problem and offers some different solutions specific to gnome-terminal
with varying success.
Best Answer
To give you the formula which involves the
wc
-based check:There is a
$LINES
shell variable which can also be used:But
$LINES
is updated only when at the command prompt. To understand what I mean, run this and resize the terminal window during thesleep
: