简体   繁体   English

使用sed替换具有匹配模式的行

[英]Replace a line with a matching pattern using sed

I want to replace this line 我想替换这一行

command: [ "--no-save", "--no-restore", "--slave", "/home/app/src/work-daily.py", "20180212"]

with

command: [ "--no-save", "--no-restore", "--slave", "/home/app/src/work-daily.py", "20180305"]

20180305 is today's date where I am storing its value in a variable dated 20180305是今天的日期,我将其价值存储在变量日期中

My approach is 我的方法是

sed 's/.*command.*/"command: \[ "--no-save", "--no-restore", "--slave", "/home/app/src/work-daily.py", "$dated"\]"/' ghj.txt

where 哪里

dated=$(date +%Y%m%d)

its giving an error similar to this 它给出了类似的错误

sed: -e expression #1, char 81: unknown option to `s' sed:-e表达式#1,char 81:`s'的未知选项

Your command can be made to work with some change in the quoting and escaping: 您的命令可以在引用和转义中进行一些更改:

$ sed 's/.*command.*/command: \[ "--no-save", "--no-restore", "--slave", "\/home\/app\/src\/work-daily.py", "'"$dated"'"\]/' ghj.txt
command: [ "--no-save", "--no-restore", "--slave", "/home/app/src/work-daily.py", "20180305"]

It looks like you only want to change the last field on lines that include the string command: . 看起来您只想更改包含string command:行的最后一个字段command: In that case, the sed command can simplify to: 在这种情况下,sed命令可以简化为:

$ sed -E "/command:/ s/\"[[:digit:]]+\"\]/\"$dated\"]/" ghj.txt
command: [ "--no-save", "--no-restore", "--slave", "/home/app/src/work-daily.py", "20180305"]

Alternatively, using awk: 或者,使用awk:

$ awk -F\" -v d="$dated" '/command:/{$10=d} 1' OFS=\" ghj.txt
command: [ "--no-save", "--no-restore", "--slave", "/home/app/src/work-daily.py", "20180305"]

I'll recommend awk for this task 我会推荐awk来完成这项任务

You can substitute the last field in real time by calling date within awk 您可以通过在awk调用date来实时替换最后一个字段

$ awk -F, -v OFS=, 'BEGIN{"date +%Y%m%d" | getline d} {$NF=" \""d"\"]"}1' file
command: [ "--no-save", "--no-restore", "--slave", "/home/app/src/work-daily.py", "20180305"]

"date +%Y%m%d" | getline d; : Date would get store in d :日期将在d存储

$NF=" \\""d"\\"]" : Replacing last field with format "date"] $NF=" \\""d"\\"]" :用格式"date"]替换最后一个字段"date"]

You can use the following sed command: 您可以使用以下sed命令:

$ cat input; dated=$(date +%Y%m%d); sed "/.*command: \[ \"--no-save\", \"--no-restore\", \"--slave\", \".*work-daily.py\", \"20180212\"\]/s/201
80212/$dated/" input                                                                                                                         
command: [ "--no-save", "--no-restore", "--slave", "/home/app/src/work-daily.py", "20180212"]
command: [ "--no-save", "--no-restore", "--slave", "/home/app/src/work-daily.py", "20180305"]

where /.*command: \\[ \\"--no-save\\", \\"--no-restore\\", \\"--slave\\", \\".*work-daily.py\\", \\"20180212\\"\\]/ is used to find the proper line in the file and s/20180212/$dated/ is used to do the replacement. 其中/.*command: \\[ \\"--no-save\\", \\"--no-restore\\", \\"--slave\\", \\".*work-daily.py\\", \\"20180212\\"\\]/用于在文件中找到正确的行,并使用s/20180212/$dated/来进行替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM