Sometimes I want to sort stuff, but not the header. So, for example, when I list loaded modules in Apache, there is a 1-line header that gets included in the sort:
$ /usr/local/apache2/bin/apachectl -M | sort
alias_module (shared)
asis_module (static)
cache_disk_module (static)
cache_module (static)
core_module (static)
data_module (static)
env_module (shared)
ext_filter_module (static)
file_cache_module (static)
filter_module (shared)
headers_module (shared)
heartbeat_module (static)
heartmonitor_module (static)
http_module (static)
include_module (static)
info_module (static)
Loaded Modules:
log_config_module (shared)
macro_module (static)
mime_module (shared)
mpm_event_module (static)
ratelimit_module (static)
reqtimeout_module (shared)
setenvif_module (shared)
so_module (static)
ssl_module (static)
status_module (shared)
substitute_module (static)
unixd_module (static)
version_module (shared)
watchdog_module (static)
I tried using the -b option, but that had no effect. In any case, ignoring leading spaces would just be a workaround anyway. What I really want to do is exclude N lines of header from the sort. How can I do that?
head
implementations (ksh93'shead
builtin being an exception), that won't work properly ashead
will read its input by block so typically read more than one line of the input. Tryseq 10 | { head -1; sort; }
for instance. – Stéphane Chazelas Jan 11 '18 at 15:58apachectl
outputs the second and following lines. If it outputs them at the same time as the header (within the samewrite()
system call) or beforehead
has read the first line, then they will be eaten byhead
.line
, orIFS= read -r line
or ksh93'shead
builtin avoid the problem by reading the input one byte at a time and stop reading as soon as they see the delimiter of the first line. – Stéphane Chazelas Jan 11 '18 at 17:27