用sed或awk更改脚本文件的一部分

时间:2021-01-05 16:52:10

I'm trying to write sed command to change my script automatically. This script is using to apply SQL patches to database based on provided version of application. For better understandings I simplify this script and source looks like

我正在尝试编写sed命令来自动更改我的脚本。此脚本用于根据提供的应用程序版本将SQL修补程序应用于数据库。为了更好地理解,我简化了这个脚本和源代码

# something before
if [ "$BRANCH" = "TRUNK" ]
then
    apply_patch.sh 1.0
    apply_patch.sh 2.0
    apply_patch.sh trunk
elif [ "$BRANCH" = "2.0" ]
then
    apply_patch.sh 1.0
    apply_patch.sh 2.0
elif [ "$BRANCH" = "1.0" ]
then
    apply_patch.sh 1.0
fi
# something after

Based on two input arguments (current version and next version) I need to change this script to the following

基于两个输入参数(当前版本和下一版本),我需要将此脚本更改为以下内容

# something before
if [ "$BRANCH" = "TRUNK" ]
then
    apply_patch.sh 1.0
    apply_patch.sh 2.0
    apply_patch.sh 2.1
    apply_patch.sh trunk
elif [ "$BRANCH" = "2.1" ]
then
    apply_patch.sh 1.0
    apply_patch.sh 2.0
    apply_patch.sh 2.1
elif [ "$BRANCH" = "2.0" ]
then
    apply_patch.sh 1.0
    apply_patch.sh 2.0
elif [ "$BRANCH" = "1.0" ]
then
    apply_patch.sh 1.0
fi
# something after

3 个解决方案

#1


2  

Sketch of how you might rework your logic
I will have to check up correct array syntax
but basically stop repeating and rewriting
and just add a new element to your array when patching changes

如何修改逻辑的草图我将不得不检查正确的数组语法,但基本上停止重复和重写,只需在修补更改时向数组添加新元素

ln -s trunk 99999
declare -a appver=( 1.0 2.0 2.1 99999)

for patch in  ${appver[@]} ; do
  if [ ${BRANCH} <= ${patch} ] then 
      apply_patch.sh  ${patch}
  fi
done

#2


0  

If the version and patch numbers can be represented as decimals, then you'll probably want a helper function to compare them. There are many ways this can be done. Here is an example expanding on @tomc's "sketch":

如果版本和补丁号可以表示为小数,那么您可能需要帮助函数来比较它们。有很多方法可以做到这一点。这是一个扩展@ tomc的“草图”的例子:

#!/bin/bash

function leq {
  awk -v x="$1" -v y="$2" '
    BEGIN { if (x <= y) {exit(0)} else {exit(123)} }'
}

ln -s trunk 99999
appver=( 1.0 2.0 2.1 99999 )

BRANCH="$1"
for patch in  ${appver[@]}
do
  if leq ${BRANCH} ${patch} ; then 
    apply_patch.sh ${patch}
  fi
done

#3


0  

Your requirements are very vague and unclear but MAYBE this is what you want:

您的要求非常模糊且不清楚,但可能是您想要的:

$ cat tst.awk
/if.*TRUNK/ { inTrunk = 1 }
inTrunk     {
    if (/elif/) {
        sub(/trunk/,new,buf)
        print buf prev

        sub(/TRUNK/,new,buf)
        print buf $0

        inTrunk = 0
    }
    else {
        buf = buf $0 ORS
        prev = $0
    }
    next
}
{ print }

.

$ awk -v new=2.1 -f tst.awk file
# something before
if [ "$BRANCH" = "TRUNK" ]
then
    apply_patch.sh 1.0
    apply_patch.sh 2.0
    apply_patch.sh 2.1
    apply_patch.sh trunk
if [ "$BRANCH" = "2.1" ]
then
    apply_patch.sh 1.0
    apply_patch.sh 2.0
    apply_patch.sh 2.1
elif [ "$BRANCH" = "2.0" ]
then
    apply_patch.sh 1.0
    apply_patch.sh 2.0
elif [ "$BRANCH" = "1.0" ]
then
    apply_patch.sh 1.0
fi
# something after

#1


2  

Sketch of how you might rework your logic
I will have to check up correct array syntax
but basically stop repeating and rewriting
and just add a new element to your array when patching changes

如何修改逻辑的草图我将不得不检查正确的数组语法,但基本上停止重复和重写,只需在修补更改时向数组添加新元素

ln -s trunk 99999
declare -a appver=( 1.0 2.0 2.1 99999)

for patch in  ${appver[@]} ; do
  if [ ${BRANCH} <= ${patch} ] then 
      apply_patch.sh  ${patch}
  fi
done

#2


0  

If the version and patch numbers can be represented as decimals, then you'll probably want a helper function to compare them. There are many ways this can be done. Here is an example expanding on @tomc's "sketch":

如果版本和补丁号可以表示为小数,那么您可能需要帮助函数来比较它们。有很多方法可以做到这一点。这是一个扩展@ tomc的“草图”的例子:

#!/bin/bash

function leq {
  awk -v x="$1" -v y="$2" '
    BEGIN { if (x <= y) {exit(0)} else {exit(123)} }'
}

ln -s trunk 99999
appver=( 1.0 2.0 2.1 99999 )

BRANCH="$1"
for patch in  ${appver[@]}
do
  if leq ${BRANCH} ${patch} ; then 
    apply_patch.sh ${patch}
  fi
done

#3


0  

Your requirements are very vague and unclear but MAYBE this is what you want:

您的要求非常模糊且不清楚,但可能是您想要的:

$ cat tst.awk
/if.*TRUNK/ { inTrunk = 1 }
inTrunk     {
    if (/elif/) {
        sub(/trunk/,new,buf)
        print buf prev

        sub(/TRUNK/,new,buf)
        print buf $0

        inTrunk = 0
    }
    else {
        buf = buf $0 ORS
        prev = $0
    }
    next
}
{ print }

.

$ awk -v new=2.1 -f tst.awk file
# something before
if [ "$BRANCH" = "TRUNK" ]
then
    apply_patch.sh 1.0
    apply_patch.sh 2.0
    apply_patch.sh 2.1
    apply_patch.sh trunk
if [ "$BRANCH" = "2.1" ]
then
    apply_patch.sh 1.0
    apply_patch.sh 2.0
    apply_patch.sh 2.1
elif [ "$BRANCH" = "2.0" ]
then
    apply_patch.sh 1.0
    apply_patch.sh 2.0
elif [ "$BRANCH" = "1.0" ]
then
    apply_patch.sh 1.0
fi
# something after