Re: cut off last lines of a document
- Reply: Ralf Mardorf : "Re: cut off last lines of a document"
- In reply to: Dag-Erling_Smørgrav : "Re: cut off last lines of a document"
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 04 Sep 2023 15:35:37 UTC
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