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

Jar*_*Loo 5 bash sed echo delimiter cat

假设我有一个文件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
Run Code Online (Sandbox Code Playgroud)

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

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

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

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
Run Code Online (Sandbox Code Playgroud)

但是,它看起来像这样:

 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
Run Code Online (Sandbox Code Playgroud)

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

Ase*_*rre 9

只需使用正确的报价:

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

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

在引用更多信息点击这里