我获得了一个以这种格式构建的数据库
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
(注意第6栏中的T)
我的任务是输出数据库的行,日期从给定的dateA
开始到给定的dateB
。
到目前为止,我已尝试按第6列排序我的文件,排序为-M
,具体为sort -k 6M -t "|" "file.dat"
或sort -k6 -M -t "|"
以及其他试验。
但没有任何反应。
我需要排序,因此我可以使用awk
EDIT 例 来自此
的所需排序输出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
一定是这个
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
答案 0 :(得分:1)
最终,我没有看到这项任务有什么特别之处 - 只是简单的排序:
sort -k6,6 -t "|" 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
答案 1 :(得分:0)
添加了一些额外的数据行,使搜索示例更容易看到:
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
我将使用以下输入参数定义bash
脚本[search.sh
]:
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
实际脚本:
$ 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]}"
:将我们的bash变量传递给awk脚本;为简单起见,我们将保持相同的名称-F "|"
:为awk $6>=dateA && $6<=dateB
:仅打印其中field6位于(包括)我们的搜索日期脚本的一些示例运行:
# 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