Skip first few lines or Exclude last few lines

Hi,

OS Version below:

$: uname -a
SunOS akl0db938 5.8 Generic_Virtual sun4v sparc sun4v

I created the scripts and aliases to the script. Posting it here to check if maybe the scripts are unnecessary and there are already sed/awk/[othercommands] that can already do the same. Or perhaps anyone know of a link that discuss about selective text printing based on several scenarios besides these two that I wanted to do?

I want to be able to skip the first n lines or exclude the last n lines of a file.

These are the scripts and aliases:

$: ls -l /tmp/x*bash
-rwxr-----   1 oracle   dba          107 Jun 30 14:54 /tmp/xhead.bash
-rwxr-----   1 oracle   dba           67 Jun 30 14:41 /tmp/xtail.bash
$: alias | grep x | grep bash
xhead=/tmp/xhead.bash
xtail=/tmp/xtail.bash

==> /tmp/xhead.bash <==
#!/bin/bash
#

log=$1
exclude=$2
n=$( wc -l $log | awk '{ print $1 }' )
let n=$n-$exclude
head -${n} $log


==> /tmp/xtail.bash <==
#!/bin/bash
#

log=$1
skip=$2
let skip=$skip+1
tail +${skip} $log

Running this command to display all lines except the last 4 lines

$: xhead /tmp/x.log 4
     1  Sun Jun 30 05:00:28 2024
     2  Thread 1 advanced to log sequence 535367
     3  Sun Jun 30 05:00:28 2024
     4  ARC1: Beginning to archive log# 7 seq# 535366
     5  Sun Jun 30 05:00:28 2024
     6    Current log# 8 seq# 535367 mem# 0: /db/test/redolog/test_redo_8a.dbf
     7    Current log# 8 seq# 535367 mem# 1: /db/test/mirrlog/test_redo_8b.dbf
     8  Sun Jun 30 05:00:34 2024
     9  ARC1: Completed archiving log# 7 seq# 535366
    10  Sun Jun 30 05:00:47 2024
    11  Thread 1 advanced to log sequence 535368
    12  Sun Jun 30 05:00:47 2024
    13  ARC0: Beginning to archive log# 8 seq# 535367
    14  Sun Jun 30 05:00:47 2024
    15    Current log# 1 seq# 535368 mem# 0: /db/test/redolog/test_redo_1a.dbf
    16    Current log# 1 seq# 535368 mem# 1: /db/test/mirrlog/test_redo_1b.dbf

Running this command skip the first 4 lines and display the rest of the line. It is kinda like tail +5 except tail +5 start from line 5.

$: xtail /tmp/x.log 4
     5  Sun Jun 30 05:00:28 2024
     6    Current log# 8 seq# 535367 mem# 0: /db/test/redolog/test_redo_8a.dbf
     7    Current log# 8 seq# 535367 mem# 1: /db/test/mirrlog/test_redo_8b.dbf
     8  Sun Jun 30 05:00:34 2024
     9  ARC1: Completed archiving log# 7 seq# 535366
    10  Sun Jun 30 05:00:47 2024
    11  Thread 1 advanced to log sequence 535368
    12  Sun Jun 30 05:00:47 2024
    13  ARC0: Beginning to archive log# 8 seq# 535367
    14  Sun Jun 30 05:00:47 2024
    15    Current log# 1 seq# 535368 mem# 0: /db/test/redolog/test_redo_1a.dbf
    16    Current log# 1 seq# 535368 mem# 1: /db/test/mirrlog/test_redo_1b.dbf
    17  Sun Jun 30 05:00:53 2024
    18  ARC0: Completed archiving log# 8 seq# 535367
    19  Sun Jun 30 05:01:07 2024
    20  Thread 1 advanced to log sequence 535369

Contents of the test file /tmp/x.log below:

$: cat /tmp/x.log
     1  Sun Jun 30 05:00:28 2024
     2  Thread 1 advanced to log sequence 535367
     3  Sun Jun 30 05:00:28 2024
     4  ARC1: Beginning to archive log# 7 seq# 535366
     5  Sun Jun 30 05:00:28 2024
     6    Current log# 8 seq# 535367 mem# 0: /db/test/redolog/test_redo_8a.dbf
     7    Current log# 8 seq# 535367 mem# 1: /db/test/mirrlog/test_redo_8b.dbf
     8  Sun Jun 30 05:00:34 2024
     9  ARC1: Completed archiving log# 7 seq# 535366
    10  Sun Jun 30 05:00:47 2024
    11  Thread 1 advanced to log sequence 535368
    12  Sun Jun 30 05:00:47 2024
    13  ARC0: Beginning to archive log# 8 seq# 535367
    14  Sun Jun 30 05:00:47 2024
    15    Current log# 1 seq# 535368 mem# 0: /db/test/redolog/test_redo_1a.dbf
    16    Current log# 1 seq# 535368 mem# 1: /db/test/mirrlog/test_redo_1b.dbf
    17  Sun Jun 30 05:00:53 2024
    18  ARC0: Completed archiving log# 8 seq# 535367
    19  Sun Jun 30 05:01:07 2024
    20  Thread 1 advanced to log sequence 535369

For practical reason have the file names last!
(It's standard to many Unix commands, too).
Then in Linux the scripts become very short and versatile:

#!/bin/bash
#xhead.bash, skip the last $1 lines
 
skip=$1; shift
head -n -"$skip" "$@"
#!/bin/bash
#xtail.bash, skip the first $1 lines
 
skip=$1; shift
tail -n +"$((skip+1))" "$@"

The shift shifts out the first argument.
The "$@" passes the remaining arguments i.e. the file names. So the scripts allow multiple file names or no file names where the final commands default to stdin.

The GNU commands in Linux are mostly Posix compatible, that in Solaris is /usr/xpg4/bin/
You might want to set a Posix PATH in the scripts:

PATH=/usr/xpg4/bin:/bin:/usr/bin

The following scripts use sed:

#!/bin/bash
#xhead.bash, skip the last $1 lines, $1 > 0
 
skip=$1; shift
sed -n "
:L
1,$skip{
N;bL
}
P;N;D
" "$@"
#!/bin/bash
#xtail.bash, skip the first $1 lines, $1 > 0
 
skip=$1; shift
sed "1,$skip d" "$@"

Will give your script a try. Thanks a lot.