删除线路断裂,然后在文本文件中进行空间
我有一个文本文件,其中包含我试图更容易读取的数据。以下的某些行(例如INFO 2)在多行上有结果,其中有一个折断,然后是许多空间(请参见下文)。
info 1 : holiday
info 2: today the weather is very \n\r
hot
我想删除所有线路断路,然后有空间。我尝试使用
tr '\n\r ' ' ' < test.txt
,但这删除了所有线路结尾。有没有办法仅删除那些线结尾,然后是空间?我有很多小文件要循环。
在此先感谢您的任何帮助!
I have a text file that contains data that I am trying to make more easily readable. Some of the lines, e.g. info 2 below, have the results over multiple lines, where there is a line break followed by a number of spaces (see below).
info 1 : holiday
info 2: today the weather is very \n\r
hot
I would like to remove all line breaks where there is a line break followed by a space. I have tried using
tr '\n\r ' ' ' < test.txt
but this removes all line endings. Is there a way to remove only those line endings followed by a space? I have quite a number of small files which I want to loop over.
Thanks in advance for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Tr
用于翻译字符。它代替了第一组中的字符中的第一组中的字符。这是一组字符,集合中的字符顺序无关紧要(太多)。是的,您必须匹配 一个newline,然后是空间并删除它们。请注意,大多数UNIX工具都可以在Newlines上使用,您必须使用在整个文件上使用的工具。例如,使用gnu sed:
tr
is for TRanslating characters. It replaces characters in the first set for characters in the second set. This is a set of characters, the order of characters in the set do not matter (that much) for tr.Yes, you have to match a newline followed by spaces and remove them. Note that most unix tools work on newlines, you have to use tools that work on the whole file. For example, with GNU sed:
尝试
在任何符号上替换“#”,该符号不存在
try
replace "#" on any symbol, that not exists in your text
使用perl:
Using perl:
您可以为此使用
sed
You could use
sed
for this您可以将所有行读取到模式空间中,然后匹配newline
\ n \ r
,并在组中捕获至少一个空间。在替换中,BackReference
\ 1
到捕获的空间。替换后的输出:
You could read all lines into the pattern space, and then match the newline
\n\r
and capture at least a single space in a group.In the replacement the backreference
\1
to the captured space.Output after the replacement: