返回列表 发帖

[其他] 【已解决】gawk:中间数据通过管道传递后出错,如何修改

本帖最后由 思想之翼 于 2025-3-19 02:18 编辑
@echo off
setlocal enabledelayedexpansion
for /l %%d in (10000,1,10099) do (
    set o=%%d
    for /l %%e in (100000,1,100099) do (
        set p=%%e
        gawk -F"=" "NR==FNR{B[i++]=$0;next}{A[$1]=$2}END{for(i=0;i<length(B);i++){print A[B[i]]}}" "Z:\QWS\SJ\!o:~1!\1234_01.txt" "Z:\SJKZYB\!p:~1!.txt" > "Z:\KZ\001\DATA.txt"
        gawk "NR%%10==1{close(\"Z:/KZ/001/\"n\"A.txt\");++n}{print>\"Z:/KZ/001/\"n\"A.txt\"}" "Z:\KZ\001\DATA.txt"
        del /q "Z:\KZ\001\DATA.txt" >nul 2>&1
        for %%a in (1 2 3 4) do (
            gawk -F"=" -v "tab=\t" "BEGIN{A[\"\t\"]=\"\t\"}NR==FNR{A[$1]=$2;next}{c=split($0,B,//);printf A[B[1]];t1=A[B[i]];for(i=2;i<=c;i++){((A[B[i]]~tab)||(t1~tab))?t=1:t=0;printf(t?\"%%s\":\" %%s\",A[B[i]]);t1=A[B[i]]};print \"\"}" "Z:\SJHY4\%%a.txt" "Z:\KZ\001\%%aA.txt" > "Z:\KZ\001\DATA2.txt"
            gawk -F "\t+" "{for(i=1;i<=NF;++i)print $i>>\"Z:/KZ/001/\"i\".txt\"}" "Z:\KZ\001\DATA2.txt"
            del /q "Z:\KZ\001\%%aA.txt" "Z:\KZ\001\DATA2.txt" >nul 2>&1
        )
    )
)
endlocalCOPY
上列代码经过修改后,去除了DATA.txt和DATA2.txt的生成和删除步骤,通过管道传递数据,减少磁盘IO。
但是存在问题,双击XX.bat运行后,出现找不到Z:\KZ\001\1A.txt ... 的问题,如何解决?
@echo off
setlocal enabledelayedexpansion
for /l %%d in (10000,1,10000) do (
    set o=%%d
    for /l %%e in (100000,1,100000) do (
        set p=%%e
        "Z:\KZ\003\gawk.exe" -F"=" "NR==FNR{B[i++]=$0;next}{A[$1]=$2}END{for(i=0;i<length(B);i++)print A[B[i]]}" "Z:\QWS\SJ\!o:~1!\1236_03.txt" "Z:\SJKZYB\!p:~1!.txt" | "Z:\KZ\003\gawk.exe" "NR%%10==1{close(\"Z:/KZ/003/\"n\"A.txt\");++n}{print>\"Z:/KZ/003/\"n\"A.txt\"}"
        for %%a in (1 2 3 4) do (
            "Z:\KZ\003\gawk.exe" -F"=" -v "tab=\t" "BEGIN{A[\"\t\"]=\"\t\"}NR==FNR{A[$1]=$2;next}{c=split($0,B,//);printf A[B[1]];t1=A[B[1]];for(i=2;i<=c;i++){t=((A[B[i]]~tab)||(t1~tab))?1:0;printf(t?\"%%s\":\" %%s\",A[B[i]]);t1=A[B[i]]};print \"\"}" "Z:\SJHY4\%%a.txt" "Z:\KZ\003\%%aA.txt" | "Z:\KZ\003\gawk.exe" -F "\t+" "{for(i=1;i<=NF;i++)print $i>>\"Z:/KZ/003/\"i\".txt\"}"
            del /q "Z:\KZ\003\%%aA.txt" >nul 2>&1
        )
    )
)
endlocalCOPY

反斜杠跟英文双引号放在一起时 ,英文双引号前面的反斜杠会被当成转义反斜杠的
\"Z:\\KZ\\001\\\"n\"A.txt\" ,带颜色的部分会被当成转义反斜杠和转义英文双引号 ,变成\" ,并不是\\" ,所以得写成\"Z:\\KZ\\001\\\\\"n\"A.txt\"
更多请参考http://www.bathome.net/thread-3451-1-1.html

TOP

回复 2# Five66
已解决,谢谢指点!
@echo off
setlocal enabledelayedexpansion
for /l %%d in (10000,1,10099) do (
    set o=%%d
    for /l %%e in (100000,1,100099) do (
        set p=%%e
        "Z:\KZ\003\gawk.exe" -F"=" "NR==FNR{B[i++]=$0;next}{A[$1]=$2}END{for(i=0;i<length(B);i++)print A[B[i]]}" "Z:\QWS\SJ\!o:~1!\1236_03.txt" "Z:\SJKZ原版\!p:~1!.txt" | "Z:\KZ\003\gawk.exe" "NR%%10==1{close(\"Z:/KZ/003/\"n\"A.txt\");++n}{print>\"Z:/KZ/003/\"n\"A.txt\"}"
        for %%a in (1 2 3 4) do (
            "Z:\KZ\003\gawk.exe" -F"=" -v "tab=\t" "BEGIN{A[\"\t\"]=\"\t\"}NR==FNR{A[$1]=$2;next}{c=split($0,B,//);printf A[B[1]];t1=A[B[1]];for(i=2;i<=c;i++){t=((A[B[i]]~tab)||(t1~tab))?1:0;printf(t?\"%%s\":\" %%s\",A[B[i]]);t1=A[B[i]]};print \"\"}" "Z:\SJHY4\%%a.txt" "Z:\KZ\003\%%aA.txt" | "Z:\KZ\003\gawk.exe" -F "\t+" "{for(i=1;i<=NF;i++)print $i>>\"Z:/KZ/003/\"i\".txt\"}"
            del /q "Z:\KZ\003\%%aA.txt" >nul 2>&1
        )
    )
)
endlocalCOPY

TOP

返回列表