简体   繁体   English

特定格式的外壳排序

[英]Shell sorting with specific format

My database is structured in this format, and I need to sort it by the 6th column.我的数据库以这种格式构建,我需要按第 6 列对其进行排序。

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) (注意第 6 列中的 T)

So far I have tried to, by sort -M and specifically sort -k 6M -t "|" "file.dat"到目前为止,我已经尝试通过sort -M和特别sort -k 6M -t "|" "file.dat" sort -k 6M -t "|" "file.dat" or sort -k6 -M -t "|" sort -k 6M -t "|" "file.dat"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

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

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:我将使用以下输入参数定义一个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

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; -v date[AB]="${date[AB]}" :将我们的 bash 变量传递给 awk 脚本; for simplicity sake we'll keep the same names为简单起见,我们将保留相同的名称
  • -F "|" : define input field separator for awk : 定义 awk 的输入字段分隔符
  • $6>=dateA && $6<=dateB : only print lines where field6 is between (inclusive) our search dates $6>=dateA && $6<=dateB :仅打印$6>=dateA && $6<=dateB位于(包括)我们搜索日期之间的行

Some 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM