删除线路断裂,然后在文本文件中进行空间

发布于 2025-02-12 01:58:28 字数 329 浏览 0 评论 0原文

我有一个文本文件,其中包含我试图更容易读取的数据。以下的某些行(例如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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

太傻旳人生 2025-02-19 01:58:28

Tr用于翻译字符。它代替了第一组中的字符中的第一组中的字符。这是一组字符,集合中的字符顺序无关紧要(太多)。

有没有办法

是的,您必须匹配 一个newline,然后是空间并删除它们。请注意,大多数UNIX工具都可以在Newlines上使用,您必须使用在整个文件上使用的工具。例如,使用gnu sed:

sed -z 's/\n\r      //'

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.

Is there a way

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:

sed -z 's/\n\r      //'
尘世孤行 2025-02-19 01:58:28

尝试

cat your_file | tr "\r\n" "#" | sed -e "s/# \+/ /g" | tr "#" "\n"

在任何符号上替换“#”,该符号不存在

try

cat your_file | tr "\r\n" "#" | sed -e "s/# \+/ /g" | tr "#" "\n"

replace "#" on any symbol, that not exists in your text

烟花易冷人易散 2025-02-19 01:58:28

使用perl:

perl -p0e  's/ *\r\n +/ /g' test.txt

Using perl:

perl -p0e  's/ *\r\n +/ /g' test.txt
夕色琉璃 2025-02-19 01:58:28

您可以为此使用sed

$ sed ':a;N;s/\(\\n\\r\)\?\n \+\(.*\)/\2/;ba' input_file
info 1 : holiday
info 2: today the weather is very hot

You could use sed for this

$ sed ':a;N;s/\(\\n\\r\)\?\n \+\(.*\)/\2/;ba' input_file
info 1 : holiday
info 2: today the weather is very hot
昵称有卵用 2025-02-19 01:58:28

您可以将所有行读取到模式空间中,然后匹配newline \ n \ r,并在组中捕获至少一个空间。

在替换中,BackReference \ 1到捕获的空间。

sed ':a;$!{N;ba};s/\n\r\([[:blank:]]\)/\1/g' file

替换后的输出:

info 1 : holiday
info 2: today the weather is very       hot

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.

sed ':a;$!{N;ba};s/\n\r\([[:blank:]]\)/\1/g' file

Output after the replacement:

info 1 : holiday
info 2: today the weather is very       hot
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文