我正在尝试使用sed编辑文本文件。文本文件实际上是以.txt格式发送到我的电子邮件的短信文本消息,但格式不是很好。在此先感谢您的任何帮助。例如,一个特定的行:
TO YOUDate : 06/12/2013 09:52:55 AMHi can u pls pick up some bread from the store. TO : Contact NameDate : 06/12/2013 10:00:10 AMI can in about 15 minutes. I'm still in a meeting.
上面的行表示如何格式化.txt文件中的其余行。我希望这些行以TO开头并以行的完成结束(直到下一个TO)。
像这样:
TO YOUDate : 06/12/2013 09:52:55 AMHi can u pls pick up some bread from the store.
TO : Contact NameDate : 06/12/2013 10:00:10 AMI can in about 15 minutes. I'm still in a meeting.
我认为以下命令对我有用,但在找到TO后会创建一个新行。
sed '/TO/ a\
new line string' myfile.txt
答案 0 :(得分:2)
这将在第二次出现TO
时插入换行符sed 's/TO/\nTO/2' myFile.txt
试验:
temp_files > cat myFile.txt
TO YOUDate : 06/12/2013 09:52:55 AMHi can u pls pick up some bread from the store. TO : Contact NameDate : 06/12/2013 10:00:10 AMI can in about 15 minutes. I'm still in a meeting.
temp_files >
temp_files > sed 's/TO/\nTO/2' myFile.txt
TO YOUDate : 06/12/2013 09:52:55 AMHi can u pls pick up some bread from the store.
TO : Contact NameDate : 06/12/2013 10:00:10 AMI can in about 15 minutes. I'm still in a meeting.
答案 1 :(得分:2)
使用python
:
>>> import re
>>> spl = "TO"
>>> strs = "TO YOUDate : 06/12/2013 09:52:55 AMHi can u pls pick up some bread from the store. TO : Contact NameDate : 06/12/2013 10:00:10 AMI can in about 15 minutes. I'm still in a meeting."
>>> lis = re.split(r'\bTO\b',strs)[1:]
for x in lis:
print "{}{}".format(spl,x)
...
TO YOUDate : 06/12/2013 09:52:55 AMHi can u pls pick up some bread from the store.
TO : Contact NameDate : 06/12/2013 10:00:10 AMI can in about 15 minutes. I'm still in a meeting.
答案 2 :(得分:1)
sed 's|TO|\nTO|g'
最后一个参数'g'将全局替换“TO”。因此,请确保该消息不包含“TO”字符串。