以增量替换文件中各行中的文本

时间:2019-04-07 13:23:46

标签: unix

我有一个包含多行的文件(行数未知)

DD0TRANSID000019021210504250003379433005533665506656000008587201902070168304000.0AK  0000L00000.00  N          01683016832019021220190212N0000.001683065570067.000000.00000.0000000000000NAcknowledgment                                                                                                                                        
DD0TRANSID000019021210505110003379433005535567606656000008587201902085381804000.0FC  0000L00000.00  N          53818538182019021220190212N0000.053818065570067.000000.00000.0000000000000NFirst Contact                                                                                                                                         
DD0TRANSID000019021210510360003379433005535568006656000008587201902085381804000.0SR  0000L00000.00  N          53818538182019021220190212N0000.053818065570067.000000.00000.0000000000000NStatus Report                                                                                                                                         

从第3位到第10位开始的每一行中都显示文本TRANSID000 我需要能够以1的增量将其替换为TRAN000066

66是我从另一个文件(例如nextcounter)获取的变量,用于存储计数器的开始。程序更新完所有行后,我应该能够捕获到最后一个数字并使用它来更新nextcounter文件。

输出

DD0TRAN00066019021210504250003379433005533665506656000008587201902070168304000.0AK  0000L00000.00  N          01683016832019021220190212N0000.001683065570067.000000.00000.0000000000000NAcknowledgment                                                                                                                                        
    DD0TRAN00067019021210505110003379433005535567606656000008587201902085381804000.0FC  0000L00000.00  N          53818538182019021220190212N0000.053818065570067.000000.00000.0000000000000NFirst Contact                                                                                                                                         
    DD0TRAN00068019021210510360003379433005535568006656000008587201902085381804000.0SR  0000L00000.00  N          53818538182019021220190212N0000.053818065570067.000000.00000.0000000000000NStatus Report                                                                                                                                         

我尝试过awk sed和perl,但是它没有给我想要的结果。 请提出建议。

3 个答案:

答案 0 :(得分:0)

当您想使用sed时,您可能希望使用循环,避免丑陋

sed '=' inputfile | sed -r '{N;s/(.*)\n(.*)(TRANSID000)(.*)/echo "\2TRAN0$((\1+65))\4"/e}'

使用awk要容易得多:

awk -v start=66 '{ sub(/TRANSID000/, "TRAN0" start++); print }' inputfile

编辑: OP要求提供replace TRANSID with TRAN0,我在修改后的解决方案中对此进行了展示。 当我查看示例输出时,不需要附加的0
另一个问题是,当计数器高于99时会发生什么。应该删除前导零之一(使用printf "%.4d"这样的结构),还是将线长再增加1?

DD0TRAN00099019...
DD0TRAN00100019...
# or
DD0TRAN00099019...
DD0TRAN000100019...

答案 1 :(得分:0)

简单循环

s=66; while read l; do echo "$l" | sed "s/TRANSID000/TRAN$(printf '%06d' $s)/" ; s=$((s+=1)); done < inputFile > outputFile; echo $s > counterFile

答案 2 :(得分:0)

Walter答案几乎是完美的,缺少必需的3-10行限制。 因此,改进后的答案是:

    awk -v start=66 'NR > 2 && NR < 11{ sub(/TRANSID000/, "TRAN0000" start++); print }' inputfile