我有2个文件,file1有字符串& file2有行。如果file2行包含xxxxx,则需要从file2的行中的file1替换第一个字符串

时间:2018-02-21 05:24:29

标签: shell

以下是带有脚本的示例file1和file2。不知道问题出在哪里。请帮忙。

File1

first line text xxxxx
second line xxxxx some more text
third line without to replace anything
line third xxxxx

文件2

first value
second value
some third value

脚本

#!/bin/sh
while read -r value1
do
    while read -r value2
    do
        if [[ "$value1" != *"xxxxx"* ]]
            then
            echo $value1
            continue
        else
            echo $value1 | sed "s/xxxxx/$value2/"
            break
        fi
    done <file2
done <file1

预期产出:

first line text first value
second line second value some more text
third line without to replace anything
line third line third 

但获得以下

first line text first value second line first value some more text third line without to replace anything third line without to replace anything third line without to replace anything line third first value

0 个答案:

没有答案