Date: Mon, 04 Sep 2023 17:35:37 +0200 From: Ralf Mardorf <ralf-mardorf@riseup.net> To: questions@freebsd.org Subject: Re: cut off last lines of a document Message-ID: <9195f2126712560684ab72fea6f434b791afc333.camel@riseup.net> In-Reply-To: <86edjet336.fsf@ltc.des.no> References: <57be5495-97f8-4f22-9ae2-cd9712596e64@nebelschwaden.de> <uy2fdgobbejnhyctyyrbhgvhutuhtbzzxwg5wukdujyhw7h25u@74ujfxm5gjlk> <CAMtcK2qbc1jQHXJ%2Bwu0F62o6aDQKYAx3kYARuQFZLHS6DJktRg@mail.gmail.com> <a9b64c1d-02a6-45c1-aaed-85e4cf622776@nebelschwaden.de> <86edjet336.fsf@ltc.des.no>
next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, 2023-09-04 at 16:39 +0200, Dag-Erling Smørgrav wrote:
> Pipe-friendly pure shell solution:
>
> drop_last_three() {
> local a b c d
> read a
> read b
> read c
> while read d ; do
> echo "$a"
> a="$b"
> b="$c"
> c="$d"
> done
> }
Hi,
this way it takes quite a while to get the last 3 lines of the
Encyclopædia Britannica ;). Let alone that I don't understand how to use
your shell function without rewriting it.
• rocketmouse@archlinux ~/Desktop
$ cat read3
#!/bin/bash
drop_last_three() {
local a b c d
read a
read b
read c
while read d ; do
a="$b"
b="$c"
c="$d"
done
echo "$a"
echo "$b"
echo "$c"
}
drop_last_three < 2.txt
• rocketmouse@archlinux ~/Desktop
$ cat 2.txt
1
2
3
4
5
6
7
8
9
10
• rocketmouse@archlinux ~/Desktop
$ time tail -3 2.txt
8
9
10
real 0m0.010s
user 0m0.002s
sys 0m0.008s
• rocketmouse@archlinux ~/Desktop
$ time ./read3
8
9
10
real 0m0.017s
user 0m0.008s
sys 0m0.010s
The above test was done on a desktop PC. On an iPad Pro I'm using
https://ish.app/ . This "app" is Alpine Linux which is build around
busybox. It doesn't matter what shell I'm using (not necessarily
busybox), iSH is always very slow. Such a workaround IMO isn't a good
idea. I still recommend to let a script test what OS is used and to use
different commands depending on the OS.
Regards,
Ralf
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?9195f2126712560684ab72fea6f434b791afc333.camel>
