搜索和使用bash在一个文件中替换变量/ sed的 [英] Search and replace variables in a file using bash/sed

查看:276
本文介绍了搜索和使用bash在一个文件中替换变量/ sed的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个bash脚本(script.sh)搜索和input.sh文件替换一些变量。但我只需要修改这是在variable_list文件present变量和其他人离开,因为它是。

variable_list

  $ {}用户
$ {} DBNAME

input.sh

 用户名= $ {}用户
密码= $ {}密码
DBNAME = $ {} DBNAME

预计输出文件

  =用户名神谕
密码= $ {}密码GT&;这条线将不会改变该变量($ {密码})不在variable_list文件
DBNAME =神谕

以下是我尝试使用该脚本,但我无法找到正确的战略经济对话前pression

script.sh

 导出用户为oracle
出口密码= oracle123
出口DBNAME =神谕变量='variable_list
而读线;

 如果[[-n $线]
 然后
  SED -iS / $线/ $线/ G'input.sh> output.sh
 科幻
完成< $变量


解决方案

这可以工作:

 #!/斌/庆典导出用户为oracle
出口密码= oracle123
出口DBNAME =神谕变量='variable_list
而读线;

 如果[[-n $线]
 然后
  EXP = $(sed的-e'S / \\ $ / \\\\&安培/克'<<<$线)
  VAR = $(SED -e'S / \\ $ {\\([^}] \\ + \\)} / \\ 1 /'<<<$线)
  SED -iS / $ EXP / $ {!VAR} / Ginput.sh
 科幻
完成< $变量

第一个 SED 前pression脱$这是一个正则表达式元字符。第二提取物仅仅是变量的名字,然后我们用间接在我国目前的shell来获得的价值,并在 SED 前pression使用它。

修改

而不是重写文件,所以很多时候,它可能更有效像这样做,为构建参数列表 SED

 #!/斌/庆典导出用户为oracle
出口密码= oracle123
出口DBNAME =神谕而读VAR

    EXP = $(sed的-e'S / \\ $ / \\\\&安培/克'<<<$ VAR)
    VAR = $(SED -e'S / \\ $ {\\([^}] \\ + \\)} / \\ 1 /'<<<$ VAR)
    ARGS + =( - (E S)/ $ EXP / $ {!VAR} / G)
完成< variable_listSED$ {ARGS [@]}input.sh> output.sh

I am trying to write a bash script(script.sh) to search and replace some variables in input.sh file. But I need to modify only the variables which are present in variable_list file and leave others as it is.

variable_list

${user}  
${dbname}

input.sh

username=${user}  
password=${password}  
dbname=${dbname}

Expected output file

username=oracle  
password=${password} > This line won't be changed as this variable(${password}) is not in variable_list file
dbname=oracle

Following is the script I am trying to use but I am not able to find the correct sed expression

script.sh

export user=oracle  
export password=oracle123  
export dbname=oracle

variable='variable_list'  
while read line ;  
do  
 if [[ -n $line ]]     
 then  
  sed -i 's/$line/$line/g' input.sh > output.sh  
 fi  
done < "$variable"    

解决方案

This could work:

#!/bin/bash

export user=oracle  
export password=oracle123  
export dbname=oracle

variable='variable_list'  
while read line ;  
do  
 if [[ -n $line ]]
 then
  exp=$(sed -e 's/\$/\\&/g' <<< "$line")
  var=$(sed -e 's/\${\([^}]\+\)}/\1/' <<< "$line")
  sed -i "s/$exp/${!var}/g" input.sh
 fi  
done < "$variable"

The first sed expression escapes the $ which is a regex metacharacter. The second extracts just the variable name, then we use indirection to get the value in our current shell and use it in the sed expression.

Edit

Rather than rewriting the file so many times, it's probably more efficient to do it like this, building the arguments list for sed:

#!/bin/bash

export user=oracle  
export password=oracle123  
export dbname=oracle

while read var
do
    exp=$(sed -e 's/\$/\\&/g' <<< "$var")
    var=$(sed -e 's/\${\([^}]\+\)}/\1/' <<< "$var")
    args+=("-e s/$exp/${!var}/g")
done < "variable_list"

sed "${args[@]}" input.sh > output.sh

这篇关于搜索和使用bash在一个文件中替换变量/ sed的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆