Windows批量提取变量名称和替换值

时间:2013-05-27 16:16:34

标签: sql string dynamic batch-file

所以我有一个看起来像这样的文件:

DECLARE   
  P_IBSP_YEAR VARCHAR2(32767);  
  P_CD_PROCESSING_YEAR VARCHAR2(32767);  
  P_CD_PROCESSING_MONTH VARCHAR2(32767);  
  ERROR_LEVEL VARCHAR2(32767);  

BEGIN   
  P_IBSP_YEAR := NULL;  
  P_CD_PROCESSING_YEAR := 2013;  
  P_CD_PROCESSING_MONTH := January;  
  ERROR_LEVEL := NULL;  

  T2_GDA.GDA.MAIN_RUN_ALL ( P_IBSP_YEAR, P_CD_PROCESSING_YEAR, P_CD_PROCESSING_MONTH, ERROR_LEVEL );  
  COMMIT;   
END;  

1-我想知道BEGIN之后有多少变量(在这种情况下有四个变量:P_IBSP_YEAR,P_CD_PROCESSING_YEAR,P_CD_PROCESSING_MONTH,ERROR_LEVEL)
2-我想用变量名的前20个字符替换它们具有的值(在“:=”之后出现的值)。

要记住的事情:

a)我不知道文件中有多少变量(可能有一个或多个)

我想要的输出示例:

DECLARE   
  P_IBSP_YEAR VARCHAR2(32767);  
  P_CD_PROCESSING_YEAR VARCHAR2(32767);  
  P_CD_PROCESSING_MONTH VARCHAR2(32767);  
  ERROR_LEVEL VARCHAR2(32767);  

BEGIN   
  P_IBSP_YEAR := %P_IBSP_YEAR%;  
  P_CD_PROCESSING_YEAR := %P_CD_PROCESSING_YEAR%;  
  P_CD_PROCESSING_MONTH := %P_CD_PROCESSING_MONT%;  
  ERROR_LEVEL := %ERROR_LEVEL%;  

  T2_GDA.GDA.MAIN_RUN_ALL ( P_IBSP_YEAR, P_CD_PROCESSING_YEAR, P_CD_PROCESSING_MONTH, ERROR_LEVEL );  
  COMMIT;   
END;  

[请注意,变量P_CD_PROCESSING_MONTH变为%P_CD_PROCESSING_MONT%,因为它长于20个字符]

这是我到目前为止所拥有的。它工作,但它是非常静态的,因为每次我使用另一个文件时我都需要修改我的脚本。因为我很难对变量名进行编码:

@echo off  

for /f "tokens=* delims=" %%f in ('type %sql_file%') do CALL :DOREPLACE "%%f"  

GOTO AFTER_FOR  
:DOREPLACE  

setlocal enabledelayedexpansion  
SET OUTPUT_1=%*  

set OUTPUT_2=!OUTPUT_1:P_IBSP_YEAR;=%P_IBSP_YEAR%; !  

set OUTPUT_3=!OUTPUT_2:P_CD_PROCESSING_YEAR;=%P_CD_PROCESSING_YEAR%; !  

set OUTPUT_4=!OUTPUT_3:P_CD_PROCESSING_MONTH;=%P_CD_PROCESSING_MONT%; !  

set OUTPUT_5=!OUTPUT_4:ERROR_LEVEL;=%ERROR_LEVEL%; !  

for /f "tokens=* delims=" %%g in ('ECHO %OUTPUT_5%') do ECHO %%~g>>%sql_file%.tmp  

EXIT /b  


:AFTER_FOR  

echo The modified temporary sql script now looks like this  
echo.  
type %sql_file%.tmp  

我希望看起来更像是这样:

我想要一个基于我原始文件中变量数量的变量“n”。在每个命令之后,该值n递增。这样我就不会有1,2,3 ......就像我现在这样做。但总是'n'

SET OUTPUT_1=%*

set OUTPUT_2=!OUTPUT_1:Full_var1_name;=%FIRST_20_CHARACTERS_OF_VAR1%;!

set OUTPUT_3=!OUTPUT_2:Full_var2_name;=%FIRST_20_CHARACTERS_OF_VAR2%; !

set OUTPUT_4=!OUTPUT_3:Full_var3_name;=%FIRST_20_CHARACTERS_OF_VAR3%; !

set OUTPUT_5=!OUTPUT_4:Full_var4_name;=%FIRST_20_CHARACTERS_OF_VAR4%; !

for /f "tokens=* delims=" %%g in ('ECHO %OUTPUT_5%') do ECHO %%~g>>%sql_file%.tmp

EXIT /b  

对此长信息感到抱歉。谢谢你:)。

1 个答案:

答案 0 :(得分:0)

搜索&替换为GNU sed

@echo off&setlocal
set "qfile=file.sql"

set "tfile=%temp%\%random%.tmp"
(for /f %%i in ('sed -n "/DECLARE/, /BEGIN/ s/\(.*\)\s\w.*/\1/ p" "%qfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:~0,20!"
    echo(%%i !line!
    endlocal
))>"%tfile%"
for /f "usebackqtokens=1,2" %%i in ("%tfile%") do sed -i "/BEGIN/, $ s/%%i :=.*/%%i := %%%%j%%;/" "%qfile%"
del "%tfile%"
type "%qfile%"

..输出是:

DECLARE
  P_IBSP_YEAR VARCHAR2(32767);
  P_CD_PROCESSING_YEAR VARCHAR2(32767);
  P_CD_PROCESSING_MONTH VARCHAR2(32767);
  ERROR_LEVEL VARCHAR2(32767);

BEGIN
  P_IBSP_YEAR := %P_IBSP_YEAR%;
  P_CD_PROCESSING_YEAR := %P_CD_PROCESSING_YEAR%;
  P_CD_PROCESSING_MONTH := %P_CD_PROCESSING_MONT%;
  ERROR_LEVEL := %ERROR_LEVEL%;

  T2_GDA.GDA.MAIN_RUN_ALL ( P_IBSP_YEAR, P_CD_PROCESSING_YEAR, P_CD_PROCESSING_MONTH, ERROR_LEVEL );
  COMMIT;
END;