我真的需要有关如何用实际日期替换 test.txt 行中的字符串 "date" 的帮助。我尝试了多种方法但没有运气

I really need help on how to substitute the string "date" from the test.txt line with the actual date. i tried multiple ways but no luck

test.txt 文件:

today is date

以下是我到目前为止尝试过的脚本:

test.sed 文件:

  1. s/date/$(date)/

  2. s/date/$(date +'%m\ / %d \ / %y')/

下面是我运行命令行的方式:

  1. sed -r -f test.sed test.txt

  2. sed -r -E -f test.sed test.txt

sed -e "s+date+$(date)+" test.txt。我们必须使用 + 作为分隔符,因为 date 输出包含斜杠。

您无法从脚本文件中读取命令,因为您需要 shell 来扩展 $(date) 变量。

sed -i "s/date/$(date)/g" test.txt

这会将日期字符串替换为当前日期。如果您想要更具体的日期格式,您可以向日期命令添加参数。