假設我有以下1.txt檔案:
one file.txt
two file.txt
three file.txt
four file.txt
five file.txt
sixt file.txt
seven file.txt
eight file.txt
nine file.txt
我通常使用下面的以下命令來依次重命名在以下位置列出的檔案1.txt:
awk '/\.txt/{sub(".txt", count"&")} 1' 1.txt > 2.txt
輸出是2.txt:
one file1.txt
two file2.txt
three file3.txt
four file4.txt
five file5.txt
sixt file6.txt
seven file7.txt
eight file8.txt
nine file9.txt
但是當模式為.txt.
澄清一下,偽代碼類似于:
awk '/\.txt/{sub(".txt", count"&")} 1 | <change every 4 matches> ' 1.txt > 3.txt
使得3.txt是如下:
one file.txt
two file.txt
three file.txt
four file1.txt <-here
five file.txt
sixt file.txt
seven file.txt
eight file2.txt <- here
nine file.txt
我一直在尋找網路和我的學習,但我不記得這樣的事情,我很難開始一些事情來實作這個結果。
注意:也許我只需要繼續下面的命令:
awk -v n=0 '/\.txt/{if (n ==4) sub(".txt", count"&")} 1'
uj5u.com熱心網友回復:
awk僅根據您顯示的樣本,在此處再添加 1 個變體。簡單的解釋是,檢查 line 是否為 NOT NULL AND count 變數值是否為 4,然后將 .txt 替換為 count1(每次增加 1)與 .txt 本身并列印該行。
awk 'NF && count==4{sub(/\.txt/, count1"&");count=0} 1' 1.txt > 2.txt
uj5u.com熱心網友回復:
你快到了。請你試試:
awk '/\.txt/ {if ( n%4==0) sub(".txt", count"&")} 1' 1.txt > 2.txt
條件 n%4==0滿足每四個有效行。
uj5u.com熱心網友回復:
另一種選擇可能是傳遞n=4以將其用于模數和除法。
對于字串,您可以傳遞s=".txt"給index()以檢查它是否存在,以及在替換中使用 sub 。
awk -v str=".txt" -v nr=4 'index($0,str){if(!( i%nr)) sub(str,i/nr"&")}1' 1.txt > 2.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/317385.html
上一篇:防止時間檢查到使用時間?
下一篇:C-Execv-不兼容的指標型別
