我的数据格式如下:
This is line1 <line 1>.
This is line2 <http://<line2> .
This is line3 <http://<>line3>.
This is line4 <line4> .
我想将此数据转换为以下格式:
#@ <line 1>
This is line1.
#@ <http://<line2>
This is line2.
#@ <<http://<>line3>
This is line3.
#@ <line4>
This is line4.
我在python中尝试通过拆分&lt;但它没有解决我的目的'&lt;'和'&gt;'字符串本身存在。 在python或linux(sed等)中有什么方法可以实现上面给出的转换
答案 0 :(得分:3)
仅在第一个<
分割:
with open('foo.txt') as f:
for line in f:
a, b = line.split('<', 1)
b = '#@ <' + b.rstrip('. \n')
print b
print a.rstrip() + '.'
<强>输出:强>
#@ <line 1>
This is line1.
#@ <http://<line2>
This is line2.
#@ <http://<>line3>
This is line3.
#@ <line4>
This is line4.
答案 1 :(得分:1)
sed 's/\(.*\)\(<line[0-9]\{1,\}>\)./#@ \2\
\1./' YourFile