【问题标题】:Bash - how to preserve newline in sed command output?Bash - 如何在 sed 命令输出中保留换行符?
【发布时间】:2014-09-26 16:20:45
【问题描述】:

假设我有一个 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

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

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

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

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

但是,它看起来像这样:

 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

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

【问题讨论】:

    标签: bash sed echo delimiter cat


    【解决方案1】:

    只要使用正确的引用:

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

    由于\nIFS 默认值的一部分,因此不带双引号将其删除。

    更多关于引用here的信息

    【讨论】:

    • +1 但您当然可以将其大幅重构为 sed -n "/$TITLE/s/:/, /gp" BookDB.txt
    • @tripleee 确实。正如 anubhava 已经指出的那样,我只是强调了引用问题(这可能会在 OP 的代码中导致更多问题)
    • 如何在扩展变量时达到同样的效果?我需要双引号来扩展和单引号来保持新行......
    【解决方案2】:

    你可以避开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
    

    【讨论】:

      【解决方案3】:

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

      awk -F: -v OFS=', ' '$1 ~ /Potter/ { $1 = $1; print }' 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
      

      这没有输出:

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

      但这会:

      awk -F: -v OFS=', ' '$2 ~ /Rowling/ { $1 = $1; print }' 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
      

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

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

      如果有任何验证,则匹配:

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

      【讨论】:

        猜你喜欢
        • 2017-04-09
        • 2021-02-01
        • 2014-04-01
        • 2019-10-01
        • 1970-01-01
        • 2014-06-13
        • 2017-03-16
        • 1970-01-01
        • 2018-11-22
        相关资源
        最近更新 更多