我正在回圈遍歷數十萬個 CSV 檔案以從中生成更多檔案。要求是從每個檔案中提取前 1 個月、3 個月、一個月、1 年和 2 年的資料并從中生成新檔案。
我寫了下面的腳本來完成作業,但速度非常慢。這個腳本需要非常頻繁地運行,這讓我的生活變得很麻煩。有沒有更好的方法來實作我想要的結果或可能提高這個腳本的性能?
for k in *.csv; do
sed -n '/'"$(date -d "2 year ago" ' %Y-%m')"'/,$p' ${k} > temp_data_store/${k}.2years.csv
sed -n '/'"$(date -d "1 year ago" ' %Y-%m')"'/,$p' ${k} > temp_data_store/${k}.1year.csv
sed -n '/'"$(date -d "6 month ago" ' %Y-%m')"'/,$p' ${k} > temp_data_store/${k}.6months.csv
sed -n '/'"$(date -d "3 month ago" ' %Y-%m')"'/,$p' ${k} > temp_data_store/${k}.3months.csv
sed -n '/'"$(date -d "1 month ago" ' %Y-%m')"'/,$p' ${k} > temp_data_store/${k}.1month.csv
done
uj5u.com熱心網友回復:
您將每個 CSV 閱讀五次。最好只讀取每個 CSV 一次。
您多次提取相同的資料。除了一個部分之外的所有部分都是其他部分的子集。
- 2年前是1年前、6個月前、3個月前和1個月前的子集。
- 1 年前是 6 個月前、3 個月前和 1 個月前的子集。
- 6 個月前是 3 個月前和 1 個月前的子集。
- 3 個月前是 1 個月前的子集。
這意味著“2years.csv”中的每一行也在“1year.csv”中。因此,從“1year.csv”中提取“2years.csv”就足夠了。您可以使用 級聯不同的搜索tee。
以下假設您的檔案內容按時間順序排列。(我稍微簡化了參考)
sed -n "/$(date -d '1 month ago' ' %Y-%m')/,\$p" "${k}" |
tee temp_data_store/${k}.1month.csv |
sed -n "/$(date -d '3 month ago' ' %Y-%m')/,\$p" |
tee temp_data_store/${k}.3months.csv |
sed -n "/$(date -d '6 month ago' ' %Y-%m')/,\$p" |
tee temp_data_store/${k}.6months.csv |
sed -n "/$(date -d '1 year ago' ' %Y-%m')/,\$p" |
tee temp_data_store/${k}.1year.csv |
sed -n "/$(date -d '2 year ago' ' %Y-%m')/,\$p" > temp_data_store/${k}.2years.csv
uj5u.com熱心網友回復:
當前與性能相關的問題:
- 讀取每個輸入檔案 5 次 => 我們希望將其限制為每個輸入檔案一次讀取
date為每個輸入檔案(不必要)呼叫5 次(必要)=>date在for k in *.csv回圈之前進行5 次呼叫[注意:date與輸入檔案的重復讀取相比,重復呼叫的開銷將顯得蒼白無力]
假設:
- 輸入檔案以逗號分隔(否則需要調整建議的解決方案)
- 要測驗的日期的格式
YYYY-MM-... - 要測驗的資料是逗號分隔的輸入檔案的第一個欄位(否則需要調整建議的解決方案)
- 輸出檔案名前綴是輸入檔案名,沒有
.csv
樣本輸入:
$ cat input.csv
2019-09-01,line 1
2019-10-01,line 2
2019-10-03,line 3
2019-12-01,line 4
2020-05-01,line 5
2020-10-01,line 6
2020-10-03,line 7
2020-12-01,line 8
2021-03-01,line 9
2021-04-01,line 10
2021-05-01,line 11
2021-07-01,line 12
2021-09-01,line 13
2021-09-01,line 14
2021-10-11,line 15
2021-10-12,line 16
我們只需要進行一次日期計算,因此我們將在bashOPfor k in *.csv回圈中和之前執行此操作:
# date as of writing this answer: 2021-10-12
$ yr2=$(date -d "2 year ago" ' %Y-%m')
$ yr1=$(date -d "1 year ago" ' %Y-%m')
$ mon6=$(date -d "6 month ago" ' %Y-%m')
$ mon3=$(date -d "3 month ago" ' %Y-%m')
$ mon1=$(date -d "1 month ago" ' %Y-%m')
$ typeset -p yr2 yr1 mon6 mon3 mon1
declare -- yr2="2019-10"
declare -- yr1="2020-10"
declare -- mon6="2021-04"
declare -- mon3="2021-07"
declare -- mon1="2021-09"
一個awk想法(替換sedOP 當前for k in *.csv回圈中的所有呼叫):
# determine prefix to be used for output files ...
$ k=input.csv
$ prefix="${k//.csv/}"
$ echo "${prefix}"
input
awk -v yr2="${yr2}" \
-v yr1="${yr1}" \
-v mon6="${mon6}" \
-v mon3="${mon3}" \
-v mon1="${mon1}" \
-v prefix="${prefix}" \
-F ',' ' # define input field delimiter as comma
{ split($1,arr,"-") # date to be compared is in field #1
testdate=arr[1] "-" arr[2]
if ( testdate >= yr2 ) print $0 > prefix".2years.csv"
if ( testdate >= yr1 ) print $0 > prefix".1year.csv"
if ( testdate >= mon6 ) print $0 > prefix".6months.csv"
if ( testdate >= mon3 ) print $0 > prefix".3months.csv"
if ( testdate >= mon1 ) print $0 > prefix".1month.csv"
}
' "${k}"
NOTE: awk can dyncially process the input filename to determine the filename prefix (see FILENAME variable) but would still need to know the target directory name (assuming writing to a different directory from where the input file resides)
This generates the following files:
for f in "${prefix}".*.csv
do
echo "############# ${f}"
cat "${f}"
echo ""
done
############# input.2years.csv
2019-10-01,line 2
2019-10-03,line 3
2019-12-01,line 4
2020-05-01,line 5
2020-10-01,line 6
2020-10-03,line 7
2020-12-01,line 8
2021-03-01,line 9
2021-04-01,line 10
2021-05-01,line 11
2021-07-01,line 12
2021-09-01,line 13
2021-09-01,line 14
2021-10-11,line 15
2021-10-12,line 16
############# input.1year.csv
2020-10-01,line 6
2020-10-03,line 7
2020-12-01,line 8
2021-03-01,line 9
2021-04-01,line 10
2021-05-01,line 11
2021-07-01,line 12
2021-09-01,line 13
2021-09-01,line 14
2021-10-11,line 15
2021-10-12,line 16
############# input.6months.csv
2021-04-01,line 10
2021-05-01,line 11
2021-07-01,line 12
2021-09-01,line 13
2021-09-01,line 14
2021-10-11,line 15
2021-10-12,line 16
############# input.3months.csv
2021-07-01,line 12
2021-09-01,line 13
2021-09-01,line 14
2021-10-11,line 15
2021-10-12,line 16
############# input.1month.csv
2021-09-01,line 13
2021-09-01,line 14
2021-10-11,line 15
2021-10-12,line 16
Additional performance improvements:
- [especially for largish files] read from one filesystem, write to a 2nd/different filesystem; even better would be a separate filesystem for each of the 5x different output files - would require a minor tweak to the
awksolution - processing X number of input files in parallel, eg,
awkcode could be placed in abashfunction and then called via<function_name> <input_file> &; this can be done viabashloop controls,parallel,xargs, etc - 如果運行并行操作,則需要主要基于磁盤子系統吞吐量來限制并行操作的數量,即磁盤子系統在由于讀/寫爭用而變慢之前可以處理多少并發讀/寫
uj5u.com熱心網友回復:
如果您有 GNU Parallel(版本 >= 20191022),這樣的事情可能會起作用:
do_one() {
cat "$1" | parallel --pipe --tee sed -n '/$(date -d {}" ago" ' %Y-%m')/,\$p' "> temp_data_store/$1.{}.csv" \
::: "2 year" "1 year" "6 month" "3 month" "1 month"
}
export -f do_one
parallel -j1 do_one ::: *.csv
如果您的磁盤速度很快:洗掉-j1.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/314567.html
上一篇:使用bash從dig列印特定元素
