[英]Inserting a char at particular index of line(s) in a file using awk or sed
Is there a Linux way (awk, sed, grep etc) to insert a character at particular index in each line in a file? 是否有Linux方法(awk,sed,grep等)在文件的每一行的特定索引处插入字符?
For example below is the content of file.txt 例如下面是file.txt的内容
0067011990999991950051507004+68750+023550FM-12+038299999V0203301N00671220001CN9999999N9+00001+99999999999
0043011990999991950051512004+68750+023550FM-12+038299999V0203201N00671220001CN9999999N9+00221+99999999999
I want to insert a comma at given indexes (say 15,19,88 and 93) of each line in file.txt, so that the resultant would like : 我想在file.txt中的每一行的给定索引处(例如15,19,88和93)插入一个逗号,以便结果如下:
006701199099999,1950,051507004+68750+023550FM-12+038299999V0203301N00671220001CN9999999N9+,00001,+99999999999
004301199099999,1950,051512004+68750+023550FM-12+038299999V0203201N00671220001CN9999999N9+,00221,+99999999999
simpliest in sed sed中最简单
sed 's/\(.\{93\}\)/&,/;s/\(.\{88\}\)/&,/;s/\(.\{19\}\)/&,/;s/\(.\{15\}\)/&,/' YourFile
order is important or you need to keep in mind added character for next change 顺序很重要,或者您需要记住添加字符以进行下一次更改
you could do it at once but need some interpretation 您可以立即执行,但需要一些解释
sed 's/\(.\{15\}\)\(.\{4\}\)\(.\{69\}\)\(.\{5\}\)/\1,\2,\3,\4/' YourFile
这可能对您有用(GNU sed):
sed 's/./&,/93;s/./&,/88;s/./&,/19;s/./&,/15' file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.