简体   繁体   English

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

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

I'm attempting to use sed to edit a text file. 我正在尝试使用sed编辑文本文件。 The text file is actually an sms text message that was sent to my email in a .txt format, but the formatting is not pretty. 该文本文件实际上是一条短信,已以.txt格式发送到我的电子邮件中,但是格式不正确。 Thanks in advance for any assistance. 在此先感谢您的协助。 For instance, a particular line: 例如,特定的一行:

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.

The above lines represent how the rest of the lines in the .txt file is formatted. 上面的几行表示.txt文件中其余各行的格式。 I would like the lines to start with TO and end with the completion of the line (until the next TO). 我希望这些行以TO开头,并以该行的结尾结束(直到下一个TO)。

Like so: 像这样:

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.

I thought the following command would work for me, but it creates a new line after it finds TO. 我以为以下命令对我有用,但是找到TO后会创建新行。

sed '/TO/ a\
new line string' myfile.txt

This will insert a newline at the second occurrence of TO 这将在第二次出现TO时插入换行符

sed 's/TO/\nTO/2' myFile.txt

test: 测试:

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.

Using python : 使用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'

The last parameter 'g' will replace "TO" globally. 最后一个参数“ g”将全局替换“ TO”。 So make sure that the message does not contain "TO" string. 因此,请确保该消息不包含“ TO”字符串。

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

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