我有一个文本文件,我想在其中的某些字符后放置定界符(修复)。
喜欢在 1-3
字符之后放置一个定界符(任何类似 ,)比 4
比 5
比 6-17 .... .
文件看起来像这样(原始的)
22503003673916972040504700538884720121008175657358045042677480191000000919046068826
我想要这样的文件
225,0,3,003673916972,0405047005388847,20121008175657,3580450426774801,91,000000919046068826,
请推荐任何可以完成这项工作的命令或脚本?
提前致谢
最佳答案
我会使用 python:
#!/usr/bin/env python
a='22503003673916972040504700538884720121008175657358045042677480191000000919046068826'
# python strings are 0-indexed, just
# add the indexes below
delim=[2, 3, 4, 16]
A=""
for n, i in enumerate(a):
if n in delim:
A+='%s,' % i
else:
A+=i
print A
输出:
$ ./delim.py
225,0,3,003673916972,040504700538884720121008175657358045042677480191000000919046068826
它也可以使用列表理解写得非常简洁:
>>> "".join([i+',' if n in delim else i for n, i in enumerate(a)])
'225,0,3,003673916972,040504700538884720121008175657358045042677480191000000919046068826'
关于linux - 如何使用命令或脚本在文本文件中放置分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15532045/