如何在批处理文件中使用sed命令

时间:2016-11-28 13:22:35

标签: batch-file cmd

我用以下命令编写了一个shell脚本:

if [[ $(echo $FILE_NAME | awk -F"_" '{print $3}') == "IMAGE" ]];
    then
echo "in if of f_name"
     F_NAME=`echo $FILE_NAME | sed 's/IMAGE/UCM/'`
echo $F_NAME 
    else
echo "in else of f_name"
    F_NAME=`echo $FILE_NAME | sed 's/FUSION_CONFIGURATION_1/UCM/'`
    fi

这搜索了作为参数的FILENAME,然后进入此if if,然后给出结果。如何在批处理文件中使用此LOGIC?

这是批处理文件中的内部或外部错误

1 个答案:

答案 0 :(得分:1)

这是我尝试将您的bash代码翻译/解释为批处理。

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Set FILE_NAME=one_two_IMAGE_four
:: Set FILE_NAME=one_two_FUSION_CONFIGURATION_1_four

:: This replaces your awk
For /f "tokens=3 Delims=_" %%A in (
 'echo:%File_Name%'
) Do Set Token3=%%A

:: Variable content is refered to by enclosing the var name in %
:: DelayedExpansion is neccessary because inside a (code block)
:: You have to use the exclamation mark instead of the percent then
If "%Token3%"=="IMAGE" (
  Set F_NAME=!FILE_NAME:IMAGE=UCM!
) Else (
  Set F_NAME=!FILE_NAME:FUSION_CONFIGURATION_1=UCM!
)
Echo FILE_NAME=%FILE_NAME%
Echo Token3   =%Token3%
Echo F_NAME   =%F_NAME%

输出

FILE_NAME=one_two_IMAGE_four
Token3   =IMAGE
F_NAME   =one_two_UCM_four

FILE_NAME=one_two_FUSION_CONFIGURATION_1_four
Token3   =FUSION
F_NAME   =one_two_UCM_four

修改
要从字符串中重新使用扩展名.zip,每个行都将执行:

Set F_NAME=%F_NAME:.zip=%
For %%A in ("%F_NAME%") Do Set F_NAME=%%~nA
Set F_NAME=%F_NAME:~0,-4%