Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.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
Linux Bash-将第一个数组中的文件字符串替换为第二个数组中的字符串_Linux_Bash_Unix_Sh - Fatal编程技术网

Linux Bash-将第一个数组中的文件字符串替换为第二个数组中的字符串

Linux Bash-将第一个数组中的文件字符串替换为第二个数组中的字符串,linux,bash,unix,sh,Linux,Bash,Unix,Sh,我有两个数组。第一个文件中填充了我想用下载的新文件替换的文件中的值greped 请注意,我不知道第一个数组的确切外观,这意味着一些值将具有\uu,其他值将具有-,而一些值将不具有这些值,并且名称后面将立即放置:(冒号) 示例阵列: 阵列1: [帐户:123 shoppingcart-1:123 notification-core_1:123 notification-dispatcher_core_1:123 notification-dispatcher-smschannel_core_1:1

我有两个数组。第一个文件中填充了我想用下载的新文件替换的文件中的值
grep
ed

请注意,我不知道第一个数组的确切外观,这意味着一些值将具有\uu,其他值将具有-,而一些值将不具有这些值,并且名称后面将立即放置(冒号)

示例阵列:

阵列1:

[帐户:123 shoppingcart-1:123 notification-core_1:123 notification-dispatcher_core_1:123 notification-dispatcher-smschannel_core_1:123]

阵列2:

[account_custom_2:124 shoppingcart_custom_2:124 notification_custom_2:124 notification-dispatcher_custom_2:124 notification-dispatcher-smschannel_custom_2:124]

这些数组是唯一的示例,有50多个值需要替换

我正在对第二个数组中的每个项目与第一个数组中的每个项目进行比较,如下所示:

file_name="<path_to_file>/file.txt"
for i in "${!array1[@]}"
do
        for j in "${!array2[@]}"
        do
                array_2_base="`echo ${array2[$j]} | awk -F_ '{print $1}'`"
                if [[ "${array1[$i]}" == *"$array_2_base"* ]]
                then
                        sed -i "s=${array1[$i]}=${array2[$j]}=g" $file_name
                 fi
        done
done
file_name=“/file.txt”
对于“${!array1[@]}”中的i
做
对于“${!array2[@]}”中的j
做
array_2_base=“`echo${array2[$j]}| awk-F_'{print$1}`”
如果[[“${array1[$i]}”==*“$array_2_base”*]]
然后
sed-i“s=${array1[$i]}=${array2[$j]}=g“$file\u name
fi
完成
完成
在这里,我只为第二个数组中的每个项添加第一部分的子字符串,以便将其与第一个数组中的项进行比较

e、 g.帐户\u自定义\u 2:124->帐户通知调度程序\u自定义\u 2:124->通知调度程序

这很好,但当通知位于notification-core\u 1:123notification-dispatcher\u core\u 1:123notification-dispatcher-smschannel\u core\u 1:123时,我会遇到问题


你能就如何解决这个问题给出建议吗?或者你能不能提出另一种解决方法?

我找到了一种解决这个问题的方法

一旦替换,我将从第一个数组中删除字符串

file_name="<path_to_file>/file.txt"
for i in "${!array1[@]}"
do
        for j in "${!array2[@]}"
        do
                array_2_base="`echo ${array2[$j]} | awk -F_ '{print $1}'`"
                if [[ "${array1[$i]}" == *"$array_2_base"* ]]
                then
                        sed -i "s=${array1[$i]}=${array2[$j]}=g" $file_name
                        delete="${array1[$i]}"
                        array1=( "${array1[@]/$delete}" )
                 fi
        done
done
file_name=“/file.txt”
对于“${!array1[@]}”中的i
做
对于“${!array2[@]}”中的j
做
array_2_base=“`echo${array2[$j]}| awk-F_'{print$1}`”
如果[[“${array1[$i]}”==*“$array_2_base”*]]
然后
sed-i“s=${array1[$i]}=${array2[$j]}=g“$file\u name
delete=“${array1[$i]}”
array1=(“${array1[@]/$delete}”)
fi
完成
完成

我找到了解决这个问题的方法

一旦替换,我将从第一个数组中删除字符串

file_name="<path_to_file>/file.txt"
for i in "${!array1[@]}"
do
        for j in "${!array2[@]}"
        do
                array_2_base="`echo ${array2[$j]} | awk -F_ '{print $1}'`"
                if [[ "${array1[$i]}" == *"$array_2_base"* ]]
                then
                        sed -i "s=${array1[$i]}=${array2[$j]}=g" $file_name
                        delete="${array1[$i]}"
                        array1=( "${array1[@]/$delete}" )
                 fi
        done
done
file_name=“/file.txt”
对于“${!array1[@]}”中的i
做
对于“${!array2[@]}”中的j
做
array_2_base=“`echo${array2[$j]}| awk-F_'{print$1}`”
如果[[“${array1[$i]}”==*“$array_2_base”*]]
然后
sed-i“s=${array1[$i]}=${array2[$j]}=g“$file\u name
delete=“${array1[$i]}”
array1=(“${array1[@]/$delete}”)
fi
完成
完成

如果数组中的项目数相等,则可以在一个循环中处理它们

for i in "${!array1[@]}"; {
    value=${array1[$i]}
    new_value=${array2[$i]}
    sed -i "s/$value/$new_value/" file
}

如果数组中的项目数相等,则可以在一个循环中处理它们

for i in "${!array1[@]}"; {
    value=${array1[$i]}
    new_value=${array2[$i]}
    sed -i "s/$value/$new_value/" file
}

点是array2元素的
,可能包括其他元素 作为子字符串,将导致不正确的更换,具体取决于订单 匹配性。 为了避免这种情况,您可以按降序对数组进行排序,以便 首先是更长的模式

假设数组中的字符串不包含制表符,则 请尝试:

file_name="<path_to_file>/file.txt"
array1=(account:123 shoppingcart-1:123 notification-core_1:123 notification-dispatcher_core_1:123 notification-dispatcher-smschannel_core_1:123)
array2=(account_custom_2:124 shoppingcart_custom_2:124 notification_custom_2:124 notification-dispatcher_custom_2:124 notification-dispatcher-smschannel_custom_2:124)

# insert the following block to sort array2 in descending order
array2=( $(for j in "${array2[@]}"; do
    array_2_base=${j%%_*}
    printf "%s\t%s\n" "$array_2_base" "$j"
done | sort -r | cut -f2-) )

# the following code will work "as is"
for i in "${!array1[@]}"
do
    for j in "${!array2[@]}"
    do
        array_2_base="`echo ${array2[$j]} | awk -F_ '{print $1}'`"
        if [[ "${array1[$i]}" == *"$array_2_base"* ]]
        then
            sed -i "s=${array1[$i]}=${array2[$j]}=g" "$file_name"
            delete="${array1[$i]}"
            array1=( "${array1[@]/$delete}" )
        fi
    done
done

点是array2元素的
,可能包括其他元素 作为子字符串,将导致不正确的更换,具体取决于订单 匹配性。 为了避免这种情况,您可以按降序对数组进行排序,以便 首先是更长的模式

假设数组中的字符串不包含制表符,则 请尝试:

file_name="<path_to_file>/file.txt"
array1=(account:123 shoppingcart-1:123 notification-core_1:123 notification-dispatcher_core_1:123 notification-dispatcher-smschannel_core_1:123)
array2=(account_custom_2:124 shoppingcart_custom_2:124 notification_custom_2:124 notification-dispatcher_custom_2:124 notification-dispatcher-smschannel_custom_2:124)

# insert the following block to sort array2 in descending order
array2=( $(for j in "${array2[@]}"; do
    array_2_base=${j%%_*}
    printf "%s\t%s\n" "$array_2_base" "$j"
done | sort -r | cut -f2-) )

# the following code will work "as is"
for i in "${!array1[@]}"
do
    for j in "${!array2[@]}"
    do
        array_2_base="`echo ${array2[$j]} | awk -F_ '{print $1}'`"
        if [[ "${array1[$i]}" == *"$array_2_base"* ]]
        then
            sed -i "s=${array1[$i]}=${array2[$j]}=g" "$file_name"
            delete="${array1[$i]}"
            array1=( "${array1[@]/$delete}" )
        fi
    done
done

您的脚本的复杂性可能会达到一个临界点。一旦您开始在Bash中处理数组,就值得考虑您的程序是否已经超过了一个简单的shell脚本(一种运行一系列命令的“胶水”语言,只需最少的数据操作或复杂的分支逻辑)。您可能需要考虑重构为Python/Perl/Ruby/AththNo-No.Script脚本等。脚本的复杂性可能达到了临界点。一旦您开始在Bash中处理数组,就值得考虑您的程序是否已经超过了一个简单的shell脚本(一种运行一系列命令的“胶水”语言,只需最少的数据操作或复杂的分支逻辑)。您可能想考虑重构为Python/Perl/Ruby/Aythth-No-Stoel-Script等。谢谢您的建议,但我不知道要下载多少项目,所以我不知道数组是否会相同。谢谢您的建议。但我不知道要下载多少项,所以我不知道数组是否相同。它似乎将
notification-core\u 1:123
替换为
notification\u custom\u 2:124
notification-dispatcher\u core\u 1:123
也替换为
notification\u custom\u 2:124
。它们是您想要的替代品吗?它似乎将
notification-core\u 1:123
替换为
notification\u custom\u 2:124
notification-dispatcher\u core\u 1:123
也替换为
notification\u custom\u 2:124
。它们是你想要的替代品吗?谢谢你的回答,我测试了它,它正在工作(第二个建议)。谢谢你的回答,我测试了它,它正在工作(第二个建议)。