1

我有以下内容:

    http://www.google.com/site,11/30/2012 6:51:30 PM
    http://www.google.com/site,10/1/2012 6:51:30 PM
    http://www.google.com/site,11/16/2012 6:51:30 PM
    http://www.google.com/site,8/1/2012 6:51:30 PM

我希望它按 MM/DD/YYYY 排序

    http://www.google.com/site,8/1/2012 6:51:30 PM
    http://www.google.com/site,10/1/2012 6:51:30 PM
    http://www.google.com/site,11/16/2012 6:51:30 PM
    http://www.google.com/site,11/30/2012 6:51:30 PM

我可以使用 sort 命令、uniq 命令、tr、sed 等。我无法访问 awk。有任何想法吗 ? sort -t "," -k1 有点工作。

4

3 回答 3

1

尝试这样做:

sort -n -t "," -k 2 file.txt

man sort
于 2012-11-12T22:56:28.933 回答
1
while read line; do
    s=$(date -d "${line#*,}" +%s)
    echo $s $line
done < input.txt | sort -n | cut -d ' ' -f2-

因此,对于每一行,从日期字符串(在逗号之后)创建自 Epoch 以来的秒数,将其用作排序键并将其从结果输出中删除。

于 2012-11-12T23:26:08.820 回答
0

我使用 sort 命令做了一些工作,发现了我的问题,这似乎有效:

    cat s.txt | sort -n -t"/" -k 4,4n -k 5,5n
    https://www.virustotal.com/,9/16/2012 8:19:00 AM
    https://www.virustotal.com/,10/6/2012 9:20:59 AM
    https://www.virustotal.com/,11/1/2012 9:20:22 AM
    https://www.virustotal.com/,11/4/2012 9:11:02 AM
    https://www.virustotal.com/,11/6/2012 8:50:27 AM
    https://www.virustotal.com/,11/12/2012 6:51:32 PM

反转:

    cat s.txt | sort -n -t"/" -k 4,4nr -k 5,5nr
    https://www.virustotal.com/,11/12/2012 6:51:32 PM
    https://www.virustotal.com/,11/6/2012 8:50:27 AM
    https://www.virustotal.com/,11/4/2012 9:11:02 AM
    https://www.virustotal.com/,11/1/2012 9:20:22 AM
    https://www.virustotal.com/,10/6/2012 9:20:59 AM
    https://www.virustotal.com/,9/16/2012 8:19:00 AM

但是,我需要在逗号之后执行此操作,因为带有更多“/”的网站不会像http://site.com/go/to/somelink,9/16/2012 8:19:00 AM

于 2012-11-13T00:04:38.377 回答