Hallo,
On Mon, 20 Jan 2003, Jan Trippler wrote:
On Son, 19 Jan 2003 at 19:14 (+0100), David Haller wrote:
On Sat, 18 Jan 2003, Jan Trippler wrote:
[...]
Ich verstehe auch nicht, warum das so kompliziert sein muss.
[head + tail]
Naja, da kann man auch gleich head und tail direkt nehmen. Klappt
aber, wie erwaehnt nicht mit stdin (bzw. pipe):
[...]
Daher ja die Verrenkungen...
Ah - jetzt habe ichs geschnallt :-)
Wie wäre es denn dann so (Fehlerprüfung nur rudimentär):
#!/bin/bash
[ -z "$1" -o -z "$2" ] && exit 1
awk ' BEGIN { for ( i = 0; i < t; i ++) l[i] = ""; }
{ if (NR <= h) print $0;
for (i = 0; i < (t - 1); i ++) l[i] = l[i+1];
l[t - 1] = $0; }
END { for ( i = 0; i < t; i ++)
if (l[i] != "") print l[i]; }
' h=$1 t=$2 -
Schick :) Ich wusste doch, dass ich nen Knoten in der Denke hatte ;)
#!/bin/bash
[ -z "$1" -o -z "$2" ] && exit 1
awk ' { if (NR >= a && NR <= e) print $0;
} ' a=$1 e=$2 -
Auch nett :)
Jetzt das Ganze noch hübsch garniert (sprich Optionen zur Steuerung
des Verhaltens und Fehlerhandling eingebaut), zusammengerührt -
fertich ist das 3-Gänge-Menü (head - mid - tail ;-).
*g* Siehe unten, noch unvollstaendig :)
Mit der
Integration des Puffers kann man auch solche Ferkeleien machen, wie:
Hole mir die zehnt- bis drittletzte Zeile.
Ohoh! Mach das mal mit ein paar 100000 Zeilen *eg* Zumindest, wenn
man aus einer Datei liest, kann man das umgehen (siehe unten:
vorgeschaltetes wc -l ;), aus ner Pipe aber wohl nicht.
P.S.: Hatte ich schon erwähnt, dass ich den awk schick finde?
Ich auch... Guckstu unten *g*:
Ich hab' zumindest den "headntail" Teil mal implementiert (relativ
sauber hoffe ich)...
Gespannt auf eure Kommentare :))
================ ~/bin/headmidtail ================
#!/usr/bin/gawk -f
#
# headmidtail version 0.0.one-half
#
# Copyleft, -middle and -right (c)2003 by
# David Haller
# except for 'getopt' function, which is
# Public domain, 1991, 1993 by arnold@gnu.org
#
# USAGE: (so far):
# headmidtail [-s N] [-e N] [-h]
# -s NUMBER display first N lines
# -e NUMBER display last N lines
# -h help
# output will (hopefully) not contain "doubled lines"
BEGIN {
PROGNAME="headmidtail";
MAX_BUF=100000; # safeguard against mem-hogging, this "allows"
# approx. 17 MB for an mbox-file, for files with
# longer lines (or binaries, ugh!), it will be
# more correspondingly.
HEAD=TAIL=0;
flines=0;
Opterr = 1; Optind = 1; # for getopt
opts = "hs:e:l:0:1:2:3:4:5:6:7:8:9:";
while( (c = getopt(ARGC, ARGV, opts)) != -1 ) {
if(c == "s") {
HEAD=Optarg;
}
else if(c == "e") {
TAIL=Optarg;
}
else if(c == "l") {
# MLINE=gensub(/^([-0-9]*),.*/, "\\1",0,Optarg);
# MLEN =gensub(/^[-0-9]*,([0-9]*)/, "\\1",0,Optarg);
# # if(MLINE < 0) { MLINE=-MLINE; }
print "Sorry: option not implemented yet" > "/dev/stderr";
usage();
exit 1;
}
else if(c == "h") {
usage(); exit 0;
}
else {
usage(); exit 1;
}
}
for( i = 1; i < Optind; i++) {
delete ARGV[i];
}
if( (ARGC - Optind) >= 2) {
print_name = 1;
}
if(!HEAD && !TAIL) { HEAD=10; TAIL=10; }
delete buf;
bufidx=0;
}
function usage() {
print "USAGE: "PROGNAME" [-s N] [-e N]"; # [-m N[,L]]";
}
function count_lines() {
wcl="wc -l "FILENAME;
wcl | getline tmp;
close(wcl);
return gensub(/^[ \t]*([0-9]+)[ \t].*/,"\\1",0,tmp);
}
function printbuf() {
for( i=bufidx; i < TAIL; i++) {
if(!buf[i]) { continue; }
print buf[i];
}
if(bufidx != 0) {
for(i=0; i 0 && TAIL > 0) {
if( (flines-NR) < TAIL) {
print arg;
}
return;
} else {
buf[bufidx] = arg;
if(bufidx > MAX_BUF) {
printbuf();
printf "\n====\nError: Buffer exceeded: %u (max %u)\n",
bufidx, MAX_BUF > "/dev/stderr";
error = 1;
exit 1;
}
bufidx++;
if(bufidx >= TAIL) {
bufidx=0;
}
return;
}
}
FILENAME != _oldfilename {
## end of file
if (_oldfilename != "") {
printbuf();
printf "\n";
}
_oldfilename = FILENAME;
delete buf;
bufidx=0;
## begin of next file
if(!system("test -f "FILENAME)) {
flines=count_lines();
}
if(print_name) {
print "==> " FILENAME " <==";
}
NR=1;
}
NR <= HEAD {
print $0;
}
NR == HEAD && TAIL > 0 {
if( (flines - HEAD -TAIL) > 0) {
print "<==>";
}
}
NR > HEAD {
push($0);
}
END {
if(!error) {
printbuf();
}
}
####################################################################
#### Library function: from $prefix/share/getopt.awk
#
# getopt --- do C library getopt(3) function in awk
#
# arnold@gnu.org
# Public domain
function getopt(argc, argv, options, optl, thisopt, i)
{
optl = length(options)
if (optl == 0) # no options given
return -1
if (argv[Optind] == "--") { # all done
Optind++
_opti = 0
return -1
} else if (argv[Optind] !~ /^-[^: \t\n\f\r\v\b]/) {
_opti = 0
return -1
}
if (_opti == 0)
_opti = 2
thisopt = substr(argv[Optind], _opti, 1)
Optopt = thisopt
i = index(options, thisopt)
if (i == 0) {
if (Opterr)
printf("%c -- invalid option\n",
thisopt) > "/dev/stderr"
if (_opti >= length(argv[Optind])) {
Optind++
_opti = 0
} else
_opti++
return "?"
}
if (substr(options, i + 1, 1) == ":") {
# get option argument
if (length(substr(argv[Optind], _opti + 1)) > 0)
Optarg = substr(argv[Optind], _opti + 1)
else
Optarg = argv[++Optind]
_opti = 0
} else
Optarg = ""
if (_opti == 0 || _opti >= length(argv[Optind])) {
Optind++
_opti = 0
} else
_opti++
return thisopt
}
================ EOF ~/bin/headmidtail ================
-dnh
--
Und? Geht es dir noch gut? Brauchst du noch keinen Netzartzt? Ich meine ja]
nur! weil die anderen immer vom Konsumieren meiner Postings so seltsame
Nebenwirkungen bekommen. Nimm aber zur Vorsicht mal ne Blaue Froschpille.
das ist die mit den Schutzstoffen. [Woko° in dag°]