Bash - 如何在 sed 命令输出中保留换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25114517/
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-18 11:01:12  来源:igfitidea点击:

Bash - how to preserve newline in sed command output?

bashsedechodelimitercat

提问by Jared Aaron Loo

Assuming I have a file BookDB.txt which stores data in the following format :

假设我有一个 BookDB.txt 文件,它以以下格式存储数据:

Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of The Ring:Johnny Dept:56.80:100:38
Three Little Pig:Andrew Lim:89.10:290:189
All About Linux:Ubuntu Team:76.00:44:144
Catch Me If You Can:Mary Ann:23.60:6:2
Python for dummies:Jared Loo:15.99:1:10

I am trying to replace the (:) delimiter with (, ) in my output. It succeeds, but removes the newline from the output. Here is my code :

我试图在我的输出中用 (, ) 替换 (:) 分隔符。它成功了,但从输出中删除了换行符。这是我的代码:

TITLE=Potter
OUTPUT=$(cat BookDB.txt | grep $TITLE)
OUTPUT1=$(sed 's/:/, /g' <<< $OUTPUT)
echo $OUTPUT1

I want my output to look like this :

我希望我的输出看起来像这样:

Harry Potter - The Half Blood Prince, J.K Rowling, 40.30, 10, 50
Harry Potter - The Phoniex, J.K Rowling. 50.00. 30. 20
Harry Potter - The Deathly Hollow, Dan Lin, 55.00, 33, 790

However, it looks like this :

但是,它看起来像这样:

 Harry Potter - The Half Blood Prince, J.K Rowling, 40.30, 10, 50 Harry Potter - The Phoniex, J.K Rowling, 50.00, 30, 20 Harry Potter - The Deathly Holl
ow, Dan Lin, 55.00, 33, 790

If anyone could share how to preserve the line break in the output, I would be very grateful!

如果有人可以分享如何在输出中保留换行符,我将不胜感激!

回答by Aserre

Just use correct quotation :

只需使用正确的报价:

TITLE=Potter
OUTPUT=$(cat BookDB.txt | grep $TITLE)
OUTPUT1=$(sed 's/:/, /g' <<< "$OUTPUT")
echo "$OUTPUT1"

As \nis part of the default value of IFS, it is removed without the double quotes.

作为IFS\n的默认值的一部分,它在没有双引号的情况下被删除。

More info on quoting here

有关在此处引用的更多信息

回答by anubhava

You can avoid catand do all in sed:

您可以避免cat并在 sed 中完成所有操作:

sed -n '/Potter/s/:/, /gp' file
Harry Potter - The Half Blood Prince, J.K Rowling, 40.30, 10, 50
Harry Potter - The Phoniex, J.K Rowling, 50.00, 30, 20
Harry Potter - The Deathly Hollow, Dan Lin, 55.00, 33, 790

回答by konsolebox

Using awk. Matching would be specific to the titles and doesn't include the author's name.

使用awk. 匹配将特定于标题,不包括作者姓名。

awk -F: -v OFS=', ' ' ~ /Potter/ {  = ; print }' file

Output:

输出:

Harry Potter - The Half Blood Prince, J.K Rowling, 40.30, 10, 50
Harry Potter - The Phoniex, J.K Rowling, 50.00, 30, 20
Harry Potter - The Deathly Hollow, Dan Lin, 55.00, 33, 790

This gives no output:

这没有输出:

awk -F: -v OFS=', ' ' ~ /Rowling/ {  = ; print }' file

But this will:

但这将:

awk -F: -v OFS=', ' ' ~ /Rowling/ {  = ; print }' file

Output:

输出:

Harry Potter - The Half Blood Prince, J.K Rowling, 40.30, 10, 50
Harry Potter - The Phoniex, J.K Rowling, 50.00, 30, 20

To match against both title and author you can have:

要匹配标题和作者,您可以:

awk -F: -v OFS=', ' ' ~ /Potter/ &&  ~ /Rowling/ {  = ; print }' file

Or to match if any validates:

或者匹配是否有任何验证:

awk -F: -v OFS=', ' ' ~ /Potter/,  ~ /Rowling/ {  = ; print }' file