读取文件时无法替换字符串

读取文件时无法替换字符串

Final.txt 如下所示:

build/create_changes.sh
build/create_changes.sh-meta.xml
src/aura/camping/camping.design
src/aura/camping/camping.design-meta.xml

代码如下所示:

if [ -e final.txt ]
then
    ARRAY=()
    while read CFILE
    do
        echo Analyzing file `basename $CFILE`
        case "$CFILE"
            in
            *.design) TYPENAME="AuraDefinitionBundle";;
            *) TYPENAME="UNKNOWN";;
        esac

        if [ "$TYPENAME" == "AuraDefinitionBundle" ]
        then
            if [ $(contains "${ARRAY[@]}" $(basename -- "$(dirname -- "$CFILE")")) != "y" ]
            then
                echo baseFile from new method cFile $CFILE
                CFILENAME="$CFILE"

                replace="src/aura/"$(basename -- "$(dirname -- "$CFILE")")
                echo checkChanges "${CFILENAME/$CFILENAME/"$replace"}"
                CFILE="${CFILENAME/$CFILENAME/"$replace"}"
                echo baseFile from after change method cFile $CFILE
            else
                continue
            fi
        fi
    done < final.txt
else
    echo Change file not found!

我可以在回显窗口中看到:

 [exec] baseFile from new method cFile src/aura/camping/camping.design
 [exec] checkChanges src/aura/camping
 [exec] baseFile from after change method cFile src/aura/camping

但文件没有改变,我什至添加了sed命令但输出没有改变:

if [ $(contains "${ARRAY[@]}" $(basename -- "$(dirname -- "$CFILE")")) != "y" ]
then
    CFILENAME="$CFILE"
    ARRAY+=($(basename -- "$(dirname -- "$CFILE")"))
    replace="src/aura/"$(basename -- "$(dirname -- "$CFILE")")
    #CFILE="${CFILENAME/$CFILENAME/"$replace"}"
    sed -i 's/$CFILENAME/$replace/' final.txt
else
    continue
fi

预期输入

build/create_changes.sh
build/create_changes.sh-meta.xml
src/aura/camping/camping.design
src/aura/camping/camping.design-meta.xml

预期产出

build/create_changes.sh
build/create_changes.sh-meta.xml
src/aura/camping
src/aura/camping/camping.design-meta.xml

答案1

我不确定我能遵循你的逻辑,但是这个

awk '/\.design$/ {sub("/[^/]*$", "")} 1 ' file
build/create_changes.sh
build/create_changes.sh-meta.xml
src/aura/camping
src/aura/camping/camping.design-meta.xml

将产生所需的输出...

答案2

如果您只想删除所有以 结尾的路径名的基本名称.design

sed 's#/[^/]*\.design$##' final.txt

要进行就地编辑,请使用sed -i上述表达式(该标志的工作方式略有不同-i,但这适用于 GNUsed等)。

正则表达式/[^/]*\.design$匹配 a/后跟任意数量的非字符以及行尾的/文字字符串。.design

相关内容