簡體   English   中英

用bash對逗號分隔的文件進行排序

[英]sorting a comma delineated file with bash

我有以下幾點:

    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有點工作。

嘗試這樣做:

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

man sort
while read line; do
    s=$(date -d "${line#*,}" +%s)
    echo $s $line
done < input.txt | sort -n | cut -d ' ' -f2-

因此,對於每一行,從日期字符串(逗號后面)創建自 Epoch 以來的秒數,將其用作排序鍵並將其從結果輸出中刪除。

我使用 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那樣工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM