Shell脚本:从一个文件中获取一个值并将其放在另一个文件中

时间:2013-09-12 15:24:42

标签: awk grep

我有2个不同路径的2个文件。我想将已安装的版本从file1复制到file2

文件1: VersionInfo.Properties

Installed Version:13.9.0-9
Previous Version:13.8.0-2

文件2: Install.sh

  #!/bin/bash
    --- #some content is there
    ----
   uninstall_and_install_rpm component 13.7.0-3
   -----
    ------

期望输出:

文件2: Install.sh

  #!/bin/bash
    --- #some content is there
    ----
   uninstall_and_install_rpm component 13.9.0-9
   -----
    ------

1 个答案:

答案 0 :(得分:2)

awk '
    NR == FNR {
        split($0,a,":")
        if (a[1] == "Installed Version") ver = a[2]
        next
    }
    /uninstall_and_install_rpm/ {$NF = ver}
    1
' VersionInfo.Properties Install.sh > Install.sh.new &&
mv Install.sh Install.sh.old &&
mv Install.sh.new Install.sh