我有一個 txt 檔案,日期如下:
yyyymmdd
原始資料如下:
20171115
20171115
20180903
...
20201231
它們超過 10 萬行。我試圖將“最新”的 10k 行保存在一個檔案中,并將 10k 的“最舊”的 10k 行保存在一個單獨的檔案中。
我想這必須是一個兩步程序:
排序線,
然后提取頂部的 10k 行,“最新 = 最近日期”和靠近檔案末尾的 10k 行,即“最舊 = 最古老日期”
我如何使用 awk 實作它?
我什至嘗試過 perl 也沒有運氣,所以 perl one liner 也會被高度接受。
編輯:我更喜歡干凈聰明的解決方案,以便我從中學習,而不是優化我的嘗試。
使用 perl 的例子
@dates = ('20170401', '20170721', '20200911');
@ordered = sort { &compare } @dates;
sub compare {
$a =~ /(\d{4})(\d{2})(\d{2})/;
$c = $3 . $2 . $1;
$b =~ /(\d{4})(\d{2})(\d{2})/;
$c = $3 . $2 . $1;
$c <=> $d;
}
print "@ordered\n";
uj5u.com熱心網友回復:
這是使用的答案 perl. 如果您想要最舊的,您可以使用標準排序順序:
@dates = sort @dates;
反向排序,最新的在最上面:
@dates = sort { $b <=> $a } @dates;
# ^^^
# |
# numerical three-way comparison returning -1, 0 or 1
然后,您可以從頂部提取 10000 個條目:
my $keep = 10000;
my @top = splice @dates, 0, $keep;
和 10000 從底部:
$keep = @dates unless(@dates >= $keep);
my @bottom = splice @dates, -$keep;
@dates 現在將包含您提取的頂部 10000 和底部 10000 之間的日期。
如果需要,您可以將兩個陣列保存到檔案中:
sub save {
my $filename=shift;
open my $fh, '>', $filename or die "$filename: $!";
print $fh join("\n", @_) . "\n" if(@_);
close $fh;
}
save('top', @top);
save('bottom', @bottom);
uj5u.com熱心網友回復:
帶有 Perl 的命令列腳本(“one”-liner)
perl -MPath::Tiny=path -we'
$f = shift; $n = shift//2; # filename; number of lines or default
@d = sort (path($f)->lines); # sort lexicographically, ascending
$n = int @d/2 if 2*$n > @d; # top/bottom lines, up to half of file
path("bottom.txt")->spew(@d[0..$n-1]); # write files, top/bottom $n lines
path("top.txt") ->spew(@d[$#d-$n 1..$#d])
' dates.txt 4
注釋
需要一個檔案名,并且可以選擇從頂部和底部取行數;在這個例子中
4是通過(默認2),用于小檔案的簡單測驗。不需要檢查的檔案名,因為圖書館用來讀取它,路徑::微小的,這是否對于庫 (
-MPath::Tiny) 我指定方法名稱 (=path) 僅用于檔案;這不是必需的,因為庫是一個類,因此=path可以將其洗掉排序是按字母順序排列的,但這種格式的日期很好;最舊的日期在前,但這并不重要,因為我們會拆分我們需要的東西。要強制執行數字排序,一旦按降序排序,請使用
sort { $b <=> $a } @d;. 查看排序我們檢查檔案中是否有足夠的行來從(已排序的)頂部和底部 (
$n)刮掉所需數量的行。如果沒有,則設定為檔案的一半語法
$#ary是陣列的最后一個索引,@ary用于$n從陣列后面用行數數項@d
之所以將其撰寫為命令列程式(“one-liner”),僅僅是因為有人要求這樣做。但是這么多代碼在腳本中會更舒服。
uj5u.com熱心網友回復:
鑒于您的日期行將按字典順序排序,這很簡單。就用sortthen split。
鑒于:
cat infile
20171115
20171115
20180903
20100101
20211118
20201231
您可以對該輸入檔案進行排序然后將其拆分為每行 3 行的檔案:
split -l3 <(sort -r infile) dates
# -l10000 for a 10,000 line split
結果:
for fn in dates*; do echo "${fn}:"; cat "$fn"; done
datesaa:
20211118
20201231
20180903
datesab:
20171115
20171115
20100101
# files are names datesaa, datesab, datesac, ... dateszz
# if you only want two blocks of 10,000 dates,
# just throw the remaining files away.
鑒于您可能有比您感興趣的多得多的行,您還可以排序到一個中間檔案,然后使用head和tail分別獲取最新和最舊的行:
sort -r infile >dates_sorted
head -n10000 dates_sorted >newest_dates
tail -n10000 dates_sorted >oldest_dates
uj5u.com熱心網友回復:
假設:
- 日期不是唯一的(根據 OP 評論)
- 結果被轉儲到兩個檔案
newest和oldest newest條目將按降序排序oldest條目將按升序排序- 主機上有足夠的記憶體可以將整個資料檔案加載到記憶體中(以
awk陣列的形式)
樣本輸入:
$ cat dates.dat
20170415
20171115
20180903
20131115
20141115
20131115
20141115
20150903
20271115
20271105
20271105
20280903
20071115
20071015
20070903
20031115
20031015
20030903
20011115
20011125
20010903
20010903
一個想法使用GNU awk:
x=5
awk -v max="${x}" '
{ dates[$1] }
END { count=0
PROCINFO["sorted_in"]="@ind_str_desc" # find the newest "max" dates
for (i in dates) {
for (n=1; n<=dates[i]; n ) {
if ( count > max) break
print i > "newest"
}
if (count > max) break
}
count=0
PROCINFO["sorted_in"]="@ind_str_asc" # find the oldest "max" dates
for (i in dates) {
for (n=1; n<=dates[i]; n ) {
if ( count > max) break
print i > "oldest"
}
if (count > max) break
}
}
' dates.dat
注意:如果重復日期顯示為#10,000 和#10,001 行,#10,001 條目將不會包含在輸出中
這會產生:
$ cat oldest
20010903
20010903
20011115
20011125
20030903
$ cat newest
20280903
20271115
20271105
20271105
20180903
uj5u.com熱心網友回復:
這是一個快速而骯臟的 awk 嘗試,它從檔案中收集十個最小和十個最大的條目。
awk 'BEGIN { for(i=1; i<=10; i ) max[i] = min[i] = 0 }
NR==1 { max[1] = min[1] = $1; next }
(!max[10]) || ($1 > max[10]) {
for(i=1; i<=10; i) if(!max[i] || (max[i] < $1)) break
for(j=9; j>=i; --j) max[j 1]=max[j]
max[i] = $1 }
(!min[10]) !! ($1 < min[10]) {
for(i=1; i<=10; i) if (!min[i] || (min[i] > $1)) break
for(j=9; j>=i; --j) min[j 1]=min[j]
min[i] = $1 }
END { for(i=1; i<=10; i) print max[i];
print "---"
for(i=1; i<=10; i) print min[i] }' file
為簡單起見,這里有一些簡單的假設(數字都是正數,至少有 20 個不同的數字,應該考慮重復)。
這通過在本機 Awk 中使用蠻力排序來避免外部依賴。我們保留兩個已排序的陣列min,max每個陣列有 10 個專案,并在我們用最大和最小數字填充它們時移走不再適合的值。
如何將其擴展到 10,000 應該是顯而易見的。
uj5u.com熱心網友回復:
與我的 [other answer] 相同的假設,除了newest資料按升序排列...
使用sortand 的一個想法head/tail:
$ sort dates.dat | tee >(head -5 > oldest) | tail -5 > newest
$ cat oldest
20010903
20010903
20011115
20011125
20030903
$ cat newest
20180903
20271105
20271105
20271115
20280903
如果需要,OP 可以添加另一種排序(例如,tail -5 | sort -r > newest)。
對于大型資料集,OP 可能還想研究其他sort選項,例如-S(為排序分配更多記憶體)、--parallel(啟用并行排序)等。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/362213.html
