繁体   English   中英

通过以TO开头的每一行来编辑文本文件

[英]Edit a text file by starting each line with TO

我正在尝试使用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

这将在第二次出现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.

使用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.
sed 's|TO|\nTO|g'

最后一个参数“ g”将全局替换“ TO”。 因此,请确保该消息不包含“ TO”字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM