【发布时间】:2021-11-24 12:33:14
【问题描述】:
假设我在下面有以下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 时,我只想每 4 个匹配项重命名一次。
澄清一下,伪代码类似于:
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'
【问题讨论】:
标签: unix awk text-processing