通过 bash/awk/sed/perl 删除特定文本后的新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8519585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:21:46  来源:igfitidea点击:

Removing new line after a particular text via bash/awk/sed/perl

bashsedawk

提问by Greenhorn

I would like to remove all the newline character that occurs after a partiular string and replace it with a tab space. Say for instance my sample.txt is as follows

我想删除特定字符串之后出现的所有换行符,并将其替换为制表符空间。比如说我的sample.txt如下

foo
bar bar bar bar some text

I would like it to be

我希望它是

foo    bar bar bar bar some text

How do I do this via bash/awk/sed. Do help.

我如何通过 bash/awk/sed 执行此操作。帮忙。

回答by Michael J. Barber

In awk:

在 awk 中:

awk '/foo$/ { printf("%s\t", 
cat input.txt | sed ':a;N;$!ba;s/foo\n/foo\t/g'
); next } 1'

回答by DejanLekic

Here is how:

方法如下:

perl -pe 's/(?<=foo)\n/\t/' input

More about why simple sed 's/foo\n/foo\t/g'does not work here: http://linuxtopia.org/online_books/linux_tool_guides/the_sed_faq/sedfaq5_009.html

更多关于为什么简单sed 's/foo\n/foo\t/g'在这里不起作用:http: //linuxtopia.org/online_books/linux_tool_guides/the_sed_faq/sedfaq5_009.html

回答by codaddict

echo -e "foo\nbar bar bar bar some text"| sed '/foo$/{N;s/\n/\t/}'
foo     bar bar bar bar some text

回答by potong

This might work for you:

这可能对你有用:

echo -e "foo\nbar bar bar bar some text"| sed '/foo$/N;s/\n/\t/'
foo     bar bar bar bar some text

Actually:

实际上:

#!/bin/sed -f
/foo/{
N
s/\n/\t/
}

The latter solution is less efficient but not by much!

后一种解决方案效率较低,但效率不高!

回答by Toby Speight

The sedversion looks like

sed版本的样子

sed -e '/foo/N;y/\n/\t/'  <sample.txt

If this is all you want to do in your sed command, you can simplify the above to a one-liner:

如果这就是您想要在 sed 命令中执行的全部操作,则可以将上述内容简化为单行:

cat file.txt | perl -p -e 's/\n/ /g'

Explanation: Nappends a newline and the next line of input; y/\n/\treplaces all newlines with tabs, or s/\n/\t/replaces the first newline with a tab.

说明:N追加一个换行符和下一行输入;y/\n/\t用制表符替换所有换行符,或用制表符s/\n/\t/替换第一个换行符。

Note that since sedis line-oriented, the newlines in the input are considered line-separators rather than as part of any line (this is in contrast e.g. to Perl).

请注意,由于sed是面向行的,因此输入中的换行符被视为行分隔符而不是任何行的一部分(这与例如 Perl 形成对比)。

回答by Sjoerd

Not all tools can do newline replacement. Perl can:

并非所有工具都可以进行换行替换。Perl 可以:

:g/^foo$/join

回答by sehe

Mmm. not sure this fits your requirements, but I'd do this in vim:

嗯。不确定这是否符合您的要求,但我会在 vim 中执行此操作:

:g/foo$/join

Or, if foo could be anywhere on a line:

或者,如果 foo 可以在一行的任何位置:

perl -pe 's/foo\n/foo\t/g' temp2.txt

Of course, :%s/foo\n/foo\t/gwill do nicely too

当然,:%s/foo\n/foo\t/g也会做得很好

回答by Vijay

243> cat temp2.txt 
foo
bar bar bar bar some text 
pearl[ncm_o12.2_int_x86.@2].244> perl -pe 's/foo\n/foo\t/g' temp2.txt
foo     bar bar bar bar some text 

below is the test.

下面是测试。

##代码##