Reputation: 49
My database is structured in this format, and I need to sort it by the 6th column.
10027|Chen|Ning|female|1982-12-08|2010-02-22T17:59:59.221+0000|1.2.9.86|Firefox
10995116908|Chen|Wei|female|1985-08-02|2010-05-2420:52:26.582+0000|27.98.244.108|Firefox
(note on the T in the 6th column)
So far I have tried to, by sort -M
and specifically sort -k 6M -t "|" "file.dat"
or sort -k6 -M -t "|"
etc.
The desired sort output from this
933|Perera|Mahinda|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
1129|Lepland|Carmen|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
4194|Do|Hα» ChΓ|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
8333|Wang|Chen|female|1980-02-02|2010-03-15T10:21:43.365+0000|1.4.16.148|Internet Explorer
8698|Liu|Chen|female|1982-05-29|2010-02-21T08:44:41.479+0000|14.103.81.196|Firefox
must be this
8698|Liu|Chen|female|1982-05-29|2010-02-21T08:44:41.479+0000|14.103.81.196|Firefox
1129|Lepland|Carmen|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
8333|Wang|Chen|female|1980-02-02|2010-03-15T10:21:43.365+0000|1.4.16.148|Internet Explorer
933|Perera|Mahinda|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
4194|Do|Hα» ChΓ|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
Upvotes: 1
Views: 93
Reputation: 35376
Added a couple extra data lines to make search examples a bit easier to see:
933|Perera|Mahinda|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
1129|Lepland|Carmen|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
4194|Do|H? Ch?|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
8333|Wang|Chen|female|1980-02-02|2010-03-15T10:21:43.365+0000|1.4.16.148|Internet Explorer
8698|Liu|Chen|female|1982-05-29|2010-02-21T08:44:41.479+0000|14.103.81.196|Firefox
4567|Kim|Lisa|female|1982-05-29|2009-02-21T08:44:41.479+0000|14.103.81.196|Firefox
1234|Axe|John|male|1982-05-29|2012-02-21T08:44:41.479+0000|14.103.81.196|Firefox
I'll define a bash
script [search.sh
] with the following input parameters:
search.sh [--born_after <dateA>] [--born_before <dateB>] -f <dbfile>
`--born_after <dateA>` : [optional] search for data records with field6 >= this search parameter; [format=YYYY-MM-DDTHH:MM:SS.sss+HHMM] [default=0000-00-00T00:00:00.000+0000]
`--born_before <dateB>` : [optional] search for data records with field6 <= this search parameter; [format=YYYY-MM-DDTHH:MM:SS.sss+HHMM] [default=9999-99-99T99:99:99.999+9999]
`-f <dbfile>` : [required] data file to search
The actual script:
$ cat search.sh
#!/bin/bash
# set default search dates, clear the dbfile variable:
dateA="0000-00-00T00:00:00.000+0000"
dateB="9999-99-99T99:99:99.999+9999"
unset dbfile
# simulate getopts so we can parse for long and short option names
while [ $# -gt 0 ]
do
case $1 in
--born-after) dateA=$2 ; shift ;;
--born-before) dateB=$2 ; shift ;;
-f) dbfile=$2 ; shift ;;
*) echo "Unexpected argument '$1'. Aborting." ; exit 1 ;;
esac
shift
done
# if we didn't get receive/parse a `-f <dbfile>` option then abort:
[[ "${dbfile}" = '' ]] && echo "Missing a dbfile. Aborting." && exit 1
# start by sorting dbfile using RomanPerekhrest's solution; then pipe results to
# an awk script to handle the 'search'
sort -k6,6 -t "|" ${dbfile} | awk -F"|" -v dateA="${dateA}" -v dateB="${dateB}" '$6>=dateA && $6<=dateB'
-v date[AB]="${date[AB]}"
: pass our bash variables into the awk script; for simplicity sake we'll keep the same names-F "|"
: define input field separator for awk$6>=dateA && $6<=dateB
: only print lines where field6 is between (inclusive) our search datesSome sample runs of the script:
# no search dates provided (ie, use defaults; display entire file (sorted))
$ search.sh -f file.dat
4567|Kim|Lisa|female|1982-05-29|2009-02-21T08:44:41.479+0000|14.103.81.196|Firefox
8698|Liu|Chen|female|1982-05-29|2010-02-21T08:44:41.479+0000|14.103.81.196|Firefox
1129|Lepland|Carmen|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
8333|Wang|Chen|female|1980-02-02|2010-03-15T10:21:43.365+0000|1.4.16.148|Internet Explorer
933|Perera|Mahinda|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
4194|Do|H? Ch?|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
1234|Axe|John|male|1982-05-29|2012-02-21T08:44:41.479+0000|14.103.81.196|Firefox
# only print records (sorted) with field6 >= 2009-10-01
$ search.sh --born-after '2009-10-01T00:00:00.000+0000' -f file.dat
8698|Liu|Chen|female|1982-05-29|2010-02-21T08:44:41.479+0000|14.103.81.196|Firefox
1129|Lepland|Carmen|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
8333|Wang|Chen|female|1980-02-02|2010-03-15T10:21:43.365+0000|1.4.16.148|Internet Explorer
933|Perera|Mahinda|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
4194|Do|H? Ch?|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
1234|Axe|John|male|1982-05-29|2012-02-21T08:44:41.479+0000|14.103.81.196|Firefox
# only print records (sorted) with field6 between 2009-10-01 and 2011-05-05
$ search.sh --born-after '2009-10-01T00:00:00.000+0000' --born-before '2011-05-05T23:59:59.999+9999' -f file.dat
8698|Liu|Chen|female|1982-05-29|2010-02-21T08:44:41.479+0000|14.103.81.196|Firefox
1129|Lepland|Carmen|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
8333|Wang|Chen|female|1980-02-02|2010-03-15T10:21:43.365+0000|1.4.16.148|Internet Explorer
933|Perera|Mahinda|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
4194|Do|H? Ch?|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
Upvotes: 0
Reputation: 92894
Eventually, I don't see nothing special in this task - just simple sorting:
sort -k6,6 -t "|" file.dat
The output:
8698|Liu|Chen|female|1982-05-29|2010-02-21T08:44:41.479+0000|14.103.81.196|Firefox
1129|Lepland|Carmen|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer
8333|Wang|Chen|female|1980-02-02|2010-03-15T10:21:43.365+0000|1.4.16.148|Internet Explorer
933|Perera|Mahinda|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox
4194|Do|Hα» ChΓ|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer
Upvotes: 2