Bash

使用 shell 腳本,通過遞增幾條記錄從文件中複製一條記錄 1000 次或 n 次

  • November 28, 2022

我在名為 abc.txt 的文件中有一條記錄,如下所示:

ID, date, timestamp, count, idcount, unit, code, Pcode, ccode, bid, vcode

12345432,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds

我想寫一個unix shell程式碼來生成更多沒有。通過僅增加列 ID、Pcode 和 ccode 以及保留的列來記錄記錄。

12345432,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds

12345433,10-11-2011,11:11:12.555,0,0,XVC_AS,12,15,20,123454323,qweds

12345434,10-11-2011,11:11:12.555,0,0,XVC_AS,12,16,21,123454323,qweds

12345435,10-11-2011,11:11:12.555,0,0,XVC_AS,12,17,22,123454323,qweds

12345436,10-11-2011,11:11:12.555,0,0,XVC_AS,12,18,23,123454323,qweds

12345437,10-11-2011,11:11:12.555,0,0,XVC_AS,12,19,24,123454323,qweds
.
.
.
.
1000 times or n times

它必須是 shell,還是可以awk?喜歡

awk -F, -v"CNT=10" 'NR==1; NR==2 {for (;CNT--;) {print; $1++;}}' OFS=, file
ID, date, timestamp, count, idcount, unit, code, Pcode, ccode, bid, vcode
12345432,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345433,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345434,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345435,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345436,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345437,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345438,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345439,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345440,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345441,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds

CNTprint用遞增的第一個欄位對第二行進行計時。

如果必須是外殼,請嘗試

CNT=10
{ read LINE; echo $LINE; read LINE; } <file
while ((CNT--))
 do    echo $LINE
 done

確保預先設置計數,否則你最終會得到一個非常大的結果文件……

我將假設問題中輸入和輸出中的空白行實際上並不存在。使用Miller ( mlr),首先清除標頭中存在的額外空白,然後創建$n單個數據記錄的副本。然後我們通過第二次 Miller 呼叫傳遞它來更新記錄,增加 named和ID列。Pcode``ccode

mlr --csv clean-whitespace then bootstrap -n "$n" file |
mlr --csv put '
   NR == 1 { @ID=$ID; @Pcode=$Pcode; @ccode=$ccode }
   NR > 1 {
       $ID    = @ID    + NR - 1;
       $Pcode = @Pcode + NR - 1;
       $ccode = @ccode + NR - 1;
   }'

給定問題中的數據,並n=10在 shell 中,這將輸出

ID,date,timestamp,count,idcount,unit,code,Pcode,ccode,bid,vcode
12345432,10-11-2011,11:11:12.555,0,0,XVC_AS,12,14,19,123454323,qweds
12345433,10-11-2011,11:11:12.555,0,0,XVC_AS,12,15,20,123454323,qweds
12345434,10-11-2011,11:11:12.555,0,0,XVC_AS,12,16,21,123454323,qweds
12345435,10-11-2011,11:11:12.555,0,0,XVC_AS,12,17,22,123454323,qweds
12345436,10-11-2011,11:11:12.555,0,0,XVC_AS,12,18,23,123454323,qweds
12345437,10-11-2011,11:11:12.555,0,0,XVC_AS,12,19,24,123454323,qweds
12345438,10-11-2011,11:11:12.555,0,0,XVC_AS,12,20,25,123454323,qweds
12345439,10-11-2011,11:11:12.555,0,0,XVC_AS,12,21,26,123454323,qweds
12345440,10-11-2011,11:11:12.555,0,0,XVC_AS,12,22,27,123454323,qweds
12345441,10-11-2011,11:11:12.555,0,0,XVC_AS,12,23,28,123454323,qweds

使用awk並忽略標頭的清理,並假設我們知道我們要更新的欄位是欄位 1、8 和 9。此外,由於awk不是 CSV 感知的,我們必須假設我們正在處理“簡單 CSV”數據,即不包含帶有嵌入逗號或換行符的欄位的數據。

awk -v n="$n" '
   BEGIN { OFS=FS="," }
   NR==1 { print; next }
   {
       for (i=1; i<=n; ++i) {
           print
           $1++; $8++; $9++
       }
   }' file

引用自:https://unix.stackexchange.com/questions/726521