【问题标题】:How do I correctly use variables with sed?如何在 sed 中正确使用变量?
【发布时间】:2022-11-13 11:14:54
【问题描述】:

我需要在 sed 命令中使用字符串变量。我的尝试是在 script.sh 中给出的,它没有做我想要的,我假设因为我的变量包含我需要 sed 评估的字符。我在 linux bash 中工作。

输入.txt

delicious.banana
gross.apple

脚本.sh

adjectives="delicious\|gross\|bearable\|yummy"
sed "s/\($adjectives\)\.//g" input.txt > output.txt

需要 output.txt

banana
apple

output.txt 当前

deliciousbanana
grossdapple

【问题讨论】:

    标签: sed


    【解决方案1】:

    非 gnu sed 不适用于 BRE(基本正则表达式模式)中的 |。我建议使用ERE(扩展正则表达式模式)使用-E,作为奖励,您可以消除所有逃跑

    adjectives="delicious|gross|bearable|yummy"
    sed -E "s/($adjectives).//g" input.txt
    
    banana
    apple
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-21
      • 2018-05-31
      • 2013-11-05
      • 2022-01-08
      • 2014-05-07
      • 2019-04-09
      • 2015-10-03
      相关资源
      最近更新 更多