【问题标题】:Command output in sed pattern [duplicate]sed模式的命令输出[重复]
【发布时间】:2020-10-02 01:05:26
【问题描述】:

是否可以以 sed 模式输出命令?

我试过这个:

sed -i 's/pattern/${python script.py}/gI' file.txt

【问题讨论】:

  • 您需要使用双引号来允许扩展,并使用$(...) 语法来评估子shell。换句话说,sed -i "s/pattern/$(python script.py)/gI" file.txt 应该可以工作。
  • 谢谢,我试过双引号,但 {} 是错误的语法来评估表达式

标签: bash sed


【解决方案1】:

是的,这是可能的。

$ cat file.txt 
pattern
pattern
aaaaa
bbbbb

$ cat python.py 
print("hello")

$ python3 python.py 
hello

$ echo $(python3 python.py)
hello

$ sed "s/pattern/$(python3 python.py)/gI" file.txt 
hello
hello
aaaaa
bbbbb

Edit: (based on the comments) adding one example of why you should not use single quote
$ sed 's/pattern/$(python3 python.py)/gI' file.txt 
$(python3 python.py)
$(python3 python.py)
aaaaa
bbbbb

Edit2: I removed the flag -i in the sed to test, it's easier you don't change your file until you have the desired result. For the final solution you should use the sed -i to have the file changed.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    相关资源
    最近更新 更多