使用bash更改文件内容

使用bash更改文件内容

我有一个关于使用 bash 操作文件内容的问题。所以我有一个 conf 文件,内容如下:

#set_var EASYRSA_REQ_COUNTRY    "US"                                                                        
#set_var EASYRSA_REQ_PROVINCE   "California"                                                                
#set_var EASYRSA_REQ_CITY       "San Francisco"                                                             
#set_var EASYRSA_REQ_ORG        "Copyleft Certificate Co"                      
#set_var EASYRSA_REQ_EMAIL      "[email protected]"                                                            
#set_var EASYRSA_REQ_OU         "My Organizational Unit"  

我想在 bash 文件中放置一行命令来自动执行从上面更改为取消注释行的过程:

set_var EASYRSA_REQ_COUNTRY    "US"                                                                        
set_var EASYRSA_REQ_PROVINCE   "Texas"                                                                
set_var EASYRSA_REQ_CITY       "Austin"                                                             
set_var EASYRSA_REQ_ORG        "Copyleft Certificate Co"                      
set_var EASYRSA_REQ_EMAIL      "[email protected]"                                                            
set_var EASYRSA_REQ_OU         "My CompanyName" 

我遇到的问题是当我尝试将内容合并到 sed 命令时出现空格和行尾:

sed -e 's/#set_var EASYRSA_REQ_COUNTRY[ \t]*"US"\n#set_var EASYRSA_REQ_PROVINCE[ \t]*"California"\n#set_var EASYRSA_REQ_CITY[ \t]*"San Francisco"\#set_var EASYRSA_REQ_ORG[ \t]*"Copyleft Certificate Co"\n#set_var EASYRSA_REQ_EMAIL[ \t]*"[email protected]"\n#set_var EASYRSA_REQ_OU[ \t]*"My Organizational Unit"\n/set_var EASYRSA_REQ_COUNTRY[ \t]*"US"\nset_var EASYRSA_REQ_PROVINCE[ \t]*"Texas"\nset_var EASYRSA_REQ_CITY[ \t]*"Austin"\set_var EASYRSA_REQ_ORG[ \t]*"Copyleft Certificate Co"\nset_var EASYRSA_REQ_EMAIL[ \t]*"[email protected]"\nset_var EASYRSA_REQ_OU[ \t]*"My CompanyName"\n/g' file.conf
 

谢谢

答案1

$wc -l file.conf

6 文件.conf

$cat file.conf

#set_var EASYRSA_REQ_COUNTRY    "US"                                                                        
#set_var EASYRSA_REQ_PROVINCE   "California"                                                                
#set_var EASYRSA_REQ_CITY       "San Francisco"                                                             
#set_var EASYRSA_REQ_ORG        "Copyleft Certificate Co"                      
#set_var EASYRSA_REQ_EMAIL      "[email protected]"                                                            
#set_var EASYRSA_REQ_OU         "My Organizational Unit"  

sed -i 's/^#set_var/set_var/; s/California/Texas/; s/San Francisco/Austin/; s/[email protected]/[email protected]/; s/My Organizational Unit/My CompanyName/; 7,$d' file.conf

$wc -l file.conf

6 文件.conf

$cat file.conf

set_var EASYRSA_REQ_COUNTRY    "US"                                                                        
set_var EASYRSA_REQ_PROVINCE   "Texas"                                                                
set_var EASYRSA_REQ_CITY       "Austin"                                                             
set_var EASYRSA_REQ_ORG        "Copyleft Certificate Co"                      
set_var EASYRSA_REQ_EMAIL      "[email protected]"                                                            
set_var EASYRSA_REQ_OU         "My CompanyName"  

相关内容