Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 以编程方式修改文本文件中的特定行_Unix_Awk_Sed - Fatal编程技术网

Unix 以编程方式修改文本文件中的特定行

Unix 以编程方式修改文本文件中的特定行,unix,awk,sed,Unix,Awk,Sed,如果您能帮我做一些我认为很简单的事情,我将不胜感激 我希望能够以编程方式更改配置文件中的某一行。具体来说,该文件如下所示 ...Other lines... # The name of the database to mount dbms.active_database=test.db ...Other lines... 我希望能够将“test.db”替换为“production.db”,等等。其他程序可能会添加或删除行,因此我无法计算一致的行号,但没有其他行包含“dbms.active\u

如果您能帮我做一些我认为很简单的事情,我将不胜感激

我希望能够以编程方式更改配置文件中的某一行。具体来说,该文件如下所示

...Other lines...
# The name of the database to mount
dbms.active_database=test.db
...Other lines...
我希望能够将“test.db”替换为“production.db”,等等。其他程序可能会添加或删除行,因此我无法计算一致的行号,但没有其他行包含“dbms.active\u database=”

我最好怎么做

多谢各位

awk -v name0fNewDb="HEythere.db"  'BEGIN{FS=OFS="="}/dbms.active_database/{$2=name0fNewDb}1'
...Other lines...
# The name of the database to mount
dbms.active_database=HEythere.db
...Other lines...
如果我对问题陈述的理解正确,这将有助于您仅在包含
“dbms.active\u database”
的行中进行更改。它将替换
=
的右侧部分
name0fNewDb
是一个变量,您可以为其分配需求的任何文本

如果我对问题陈述的理解正确,这将有助于您仅在包含
“dbms.active\u database”
的行中进行更改。它将替换
=
的右侧部分
name0fNewDb
是一个变量,您可以为其分配需求的任何文本

使用sed:

sed 's/\(^dbms\.active_database=\)test\.db/\1production.db/' file
对于sed:

sed 's/\(^dbms\.active_database=\)test\.db/\1production.db/' file

您想用什么编程语言来编辑它?使用python会很快,simplePython也可以,但是能够通过shell编程(sed或awk??)实现这一点会更好。谢谢。您想用什么编程语言来编辑它?使用python会很快,simplePython也可以,但是能够通过shell编程(sed或awk??)实现这一点会更好。非常感谢。