Have you ever only wanted to see the beginning and the end of a text file for N=10 lines from either side? You just don’t interest whatever comes between them.

1   Simple

An intuitive approach would be like:


head -$N <FILE; tail -$N <FILE

2   Complicated

The commands above is what I just have in my mind, the original method as showing below was actually moved from old wiki, YJL --wiki, per this, written at 2012/12/07 06:46:42:


# using sed
tee >(sed -n "1,${N}p") >(tail -$N) <FILE >/dev/null
# using awk
tee >(awk "NR <= $N") >(tail -$N) <FILE >/dev/null

As of now, I don’t know why I came up with this overly complicated command, perhaps it’s the remnant of doing Multi-processing on same content, but it did bring up an issue at the time.

I believe I was trying not to use two commands regardless, although it uses two Process Substitutions, which are fine, but awk or sed is actually not too simple comparing with what we have from tail.

3   Combination

I think a better version, sort of combining both, would be:


cat <(head -$N <FILE) <(-) <(tail -$N <FILE)

Again, this utilizes Process Substitutions, only in other direction and dash separator for a line separating two ends, you can replace it, <(-), with the following manual one or whatever pattern you prefer.


<(echo ----------)

4   Conclusion

Theses methods are all I can muster up, I’d believe there could be a common command could do such thing, or an elegant simple way to instruct an existing command to do such task.