使用sed删除空文件中的字符串结果
问题描述:
我有大的文本文件,其中通过编写=
然后newline
字符有时将长行分成多行。 (来自Kaggle的安然电子邮件数据)。因为即使是单词也是这样被打破的,我想用数据做一些机器学习,我想删除这些休息。据我所见,组合=\n
仅用于这些休息时间,所以如果我删除这些休息时间,我可以获得相同的信息而没有休息时间,也不会丢失任何信息。使用sed删除空文件中的字符串结果
- 我不能使用
tr
,因为它只替换1个字符,但我有两个字符来替换。 -
的
sed
命令我使用至今不得要领是:sed --in-place --quiet --regexp-extended 's/=\n//g' email_aa_edit
其中
email_aa_edit
是安然邮件数据的一部分(用分裂分裂吧)是我的输入文件。但是,这只会产生一个空文件,我不知道为什么。 Afaik=
本身不是特殊字符,换行符应该是\n
。
删除那些=\n
发生的正确方法是什么?
答
无法删除,因为通过线SED工程线换行字符,但如果你追加下一行到模式空间有可能:
sed ':a;/=$/{N;s/=\n//;ta}' file
细节:
:a; # defines a label "a"
/=$/ { # if the line ends with =
N; # append the next line to the pattern space
s/=\n//; # replace the =\n
ta # jump to label "a" when something is replaced (that's always the case
# except if the last line ends with =)
}
注意:如果您的文件使用Windows换行符序列,请将\n
更改为\r\n
。
我想用perl:'perl -pi -e's/= \ n //'email_aa_edit' – melpomene
@melpomene有趣,我确定我有相同的正则表达式来尝试使用perl,但其他参数是不同的,它不起作用。你的工作。 – Zelphir