简体   繁体   English

对日期时间的数据进行排序在UNIX中使用sort

[英]Sort data of date time Using sort in UNIX

I want to sort my text file data using sort command. 我想使用sort命令对文本文件数据进行排序。 My data are in below format. 我的数据格式如下。

01-03-17  10:30AM                 1367 data_03012017_10_30_02.csv
01-03-17  11:00AM                   32 data_03012017_11_00_02.csv
01-03-17  11:30AM                 7532 data_03012017_11_30_03.csv
01-03-17  12:00AM                 3442 data_03012017_00_00_02.csv
01-03-17  12:00PM                 9715 data_03012017_12_00_03.csv
01-03-17  12:30AM                 1753 data_03012017_00_30_00.csv
01-03-17  12:30PM                 5857 data_03012017_12_30_03.csv

Above is my file data. 以上是我的文件数据。 Please help me to sort this data. 请帮助我对这些数据进行排序。 I am getting stuck in AM & PM data. 我陷入了AM和PM数据中。 Please help on this. 请帮忙。 Using sort -n -t"," -k1.7,1.10 -k1.1,1.2 -k1.4,1.5 command data will sort by date but not with AM & PM. 使用sort -n -t"," -k1.7,1.10 -k1.1,1.2 -k1.4,1.5命令数据将按日期排序,但不按AM和PM排序。 Let me know how to do this. 让我知道该怎么做。

Assuming your data is in unsorted.dat you could use the following code 假设您的数据unsorted.dat ,可以使用以下代码

cat unsorted.dat | while read line ; do
   d=$( echo "$line" | cut -d" "  -f1-2 )
   s=$( echo "$d" | \
    sed 's/^\([0-9]*\)-\([0-9]*\)-\([0-9]*\)\(\s.*\)$/\3-\1-\2\4/' | \
        xargs -0 date +%s -d )
   echo "$s $line"
done | sort -n -k1,1 | cut -d" " -f2-

What it does is 它的作用是

  1. Read the file line by line 逐行读取文件
  2. Extract the date field from the first two columns into a variable $d 将前两列中的日期字段提取到变量$d
  3. Change the order of the fields from MM-DD-YY to YY-MM-DD as recognized by the GNU date utility 将GNU date实用程序识别的字段顺序从MM-DD-YY更改为YY-MM-DD
  4. Convert the date to seconds since 01-01-1970 将日期转换为秒(自1970年1月1日起)
  5. Add the number of seconds to the line 将秒数添加到行中
  6. Sort by the number of seconds 按秒数排序
  7. Cut the first field containing the number of seconds out. 剪切掉包含秒数的第一个字段。

Result: 结果:

01-03-17  12:00AM                 3442 data_03012017_00_00_02.csv
01-03-17  12:30AM                 1753 data_03012017_00_30_00.csv
01-03-17  10:30AM                 1367 data_03012017_10_30_02.csv
01-03-17  11:00AM                   32 data_03012017_11_00_02.csv
01-03-17  11:30AM                 7532 data_03012017_11_30_03.csv
01-03-17  12:00PM                 9715 data_03012017_12_00_03.csv
01-03-17  12:30PM                 5857 data_03012017_12_30_03.csv

I would convert your dates to something more standard. 我会将您的日期转换为更标准的日期。

Assuming you're using mm-dd-yy (an old fashioned USA convention), you might do the following using the BSD date command in POSIX or bash shell: 假设您使用的是mm-dd-yy(一种老式的美国惯例),则可以在POSIX或bash shell中使用BSD date命令执行以下操作:

$ while read d t s f; do printf "%s %20d %s\n" "$(date -jf '%m-%d-%y %I:%M%p' "$d $t" '+%F %T')" "$s" "$f"; done < input.txt | sort
2017-01-03 00:00:43                 3442 data_03012017_00_00_02.csv
2017-01-03 00:30:43                 1753 data_03012017_00_30_00.csv
2017-01-03 10:30:43                 1367 data_03012017_10_30_02.csv
2017-01-03 11:00:43                   32 data_03012017_11_00_02.csv
2017-01-03 11:30:43                 7532 data_03012017_11_30_03.csv
2017-01-03 12:00:43                 9715 data_03012017_12_00_03.csv
2017-01-03 12:30:43                 5857 data_03012017_12_30_03.csv

Or, split apart for easier reading: 或者,分开阅读以便于阅读:

while read d t s f; do
  printf "%s %20d %s\n" \
    "$(date -jf '%m-%d-%y %I:%M%p' "$d $t" '+%F %T')" \
    "$s" \
    "$f"
done < input.txt | sort

This uses the date command to interpret and reassemble your dates, converting them from your current format into something that sorts naturally. 它使用date命令来解释和重组日期,将它们从当前格式转换为自然排序的格式。 Note that by doing this, you avoid the need for ANY options for the sort command. 请注意,这样做可以避免对sort命令使用ANY选项。

Tested in OS X and FreeBSD. 在OS X和FreeBSD中测试。

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

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