Hi.
This can be thought of as an ordering problem. Usually we think of situations like this in terms of character sorts.
However, we could also specify a set of strings to be used as the ordering sequence. In this case:
To:
Date:
Subject:
So we place these strings into a sort-order file, and sort the data files. Program msort
can handle these kinds of tasks. Here is an example with 2 simple data files:
#!/usr/bin/env bash
# @(#) s1 Demonstrate sort, ordering by character strings, msort.
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C dixf
pl " Input sort-order file sort-order:"
head sort-order
pl " Input files data*:"
head data*
pl " Results:"
for j in data*
do
msort -q -j -l -n 1,1 -s sort-order $j
pe
done
pl " Some detail about msort:"
dixf msort
exit 0
producing:
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.9 (jessie)
bash GNU bash 4.3.30
dixf (local) 1.54
-----
Input sort-order file sort-order:
To:
Date:
Subject:
-----
Input files data*:
==> data1 <==
Subject: Email test 1
From: drl
To: drl
Date: Today 05:36
Hi ... cheers, drl
==> data2 <==
To: drl
Subject: Email test 1
Hi ... cheers, drl1
From: drl
Date: Today 06:36
-----
Results:
To: drl
Date: Today 05:36
Subject: Email test 1
From: drl
Hi ... cheers, drl
To: drl
Date: Today 06:36
Subject: Email test 1
From: drl
Hi ... cheers, drl1
-----
Some detail about msort:
msort sort records in complex ways (man)
Path : /usr/bin/msort
Version : 8.53
Type : ELF64-bitLSBexecutable,x86-64,version1(SYSV ...)
Help : probably available with -h,--help
Repo : Debian 8.9 (jessie)
Home : http://www.billposer.org/Software/msort.html (pm)
Home : http://billposer.org/Software/msort.html (doc)
The msort
program is slower than the usual system sorting code, sort
, but has a number of additional features that make its use very attractive.
As this demonstration illustrates, a single call to msort
handles this task. The creation (and maintenance) of the sort-order file can be done easily with a text editor.
Best wishes ... cheers, drl