返回列表 发帖

[文本处理] grep 2.22 单文件回车换行版,支持PCRE

本帖最后由 CrLf 于 2016-1-3 19:25 编辑

单文件无 .dll 依赖
支持 PCRE 正则(-P 开关)
支持 win 下的回车换行

已更新到 Batch-CN
下载地址:http://www.bathome.net/s/tool/?d ... Fgrep&keymode=3
该版本和其他版本见列表:http://www.bathome.net/s/tool/?key=grep&keymode=3

20151223 通过重定义 fwrite 的方式实现支持 \r\n 格式的换行,效率可能比原版略有降低,也未考虑超大 size 的情况
20160103 重定义 fputs/puts/fputc,使其支持 \r\n 格式的换行
实现代码:
size_t _fwrite_CrLf(const void* buffer, size_t size, size_t count, FILE* stream)
{
size_t ret=0;
size_t sc,pc;
char *p,*pbuffer,*q=buffer,lastC='\0';
pc=sc=size*count;
p=pbuffer=malloc(sc*2);
for(;sc-->0;p++,q++){
if(*q=='\n'&&lastC!='\r'){
*p='\r';
p++,pc++;
}
lastC=*p=*q;
}
*p='\0';
ret=fwrite(pbuffer, 1, pc, stream);
free(pbuffer);
return ret;
}
int _fputs_CrLf(const void* buffer, FILE* stream)
{
size_t count, ret;
count = strlen(buffer);
ret = _fwrite_CrLf(buffer, 1, count, stream);
if(ret != count) return ret;
fputc('\r', stream);
fputc('\n', stream);
return ret+2;
}
int _fputc_CrLf(const char chr, FILE* stream)
{
if(chr=='\n')
return fwrite("\r\n", 1, 2, stream) == 2 ? chr : -1;
else
return fputc(chr, stream);
}
#undef fwrite
#define fwrite(buffer, size, count, stream) _fwrite_CrLf(buffer, size, count, stream)
#undef fputs
#define fputs(buffer, stream) _fputs_CrLf(buffer, stream)
#undef puts
#define puts(buffer) _fputs_CrLf(buffer, stdout)
#undef fputc
#define fputc(chr, stream) _fputc_CrLf(chr, stream)COPY

回复 8# xp3000


    sed fr 这些工具支持通配符*.txt
fr 支持枚举子目录。
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

这个正则表达式批量替换字符串怎么写?大量txt或log,放目录执行

碰到括号数字跟着有顿号的加回车换行
下面顿号是字符串
(1)、(2)、(3)、
......
替换成
(1)、回车换行(2)、回车换行(3)、回车换行
......
替换后、后面要有换行

然后把多个连续回车换行的替换成为1个回车换行

TOP

本帖最后由 CrLf 于 2015-12-25 05:41 编辑

回复 5# aa77dd@163.com


咱好像跑题了


.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
.........................................................................................................................................................................................................
@echo off & setlocal EnableDelayedExpansion
set /a n1=400,n2=250
set /a Width=n1,Height=n1
set /a Lines=350
set /a EndLine=Lines-Height
set "Count=0"
for /l %%b in (0 4 %Width%) do (
set /a r1=%%b*%%b-n1*n1
if !r1! leq 0 (set "inBigCycle=1") else set "inBigCycle=0"
set /a r2=%%b*%%b-n2*n2
if !Count!==0 set /a rMin=r[0]=!r2!
if !r2! geq 0 if !inBigCycle!==1 (
set /a Count+=1
for %%z in (!Count!) do set "r[%%z]=!r2!"
set "rMax=!r2!"
)
)
set /a "partLen=Count/7,p1=partLen+=^!partLen,p2=2*Count/7,p3=3*Count/7,p4=4*Count/7,p5=5*Count/7,p6=6*Count/7,p7=7*Count/7,partLen+=^!partLen"
set /a p1+=!!(p7-p6-p1)*!!(p2-p1)
for /l %%z in (0 1 !Count!) do (
set /a R=G=B=0
if %%z lss !p1! set /a "R=255-255*(%%z)/(p1)"
if %%z geq !p3! if %%z lss !p4! set /a "R=(255*(%%z-p3)/(partLen))"
if %%z geq !p4! set /a "R=255"
if %%z leq !p1! if %%z lss !p2! set /a "G=255*(%%z)/(p1)"
if %%z geq !p1! if %%z lss !p2! set /a "G=255*(%%z-p1)/(p1)"
if %%z geq !p2! if %%z lss !p4! set /a "G=255"
if %%z geq !p4! if %%z lss !p7! set /a "G=255-255*(%%z-p4)/(!p3!)"
if %%z lss !p2! set /a "B=255"
if %%z geq !p2! if %%z lss !p3! set /a "B=255-255*(%%z-p2)/(partLen)"
set /a n=R*256*256+G*256+B
cmd /c "exit /b %%n%%"
set "RGB[%%z]=#!=ExitCode:~-6!"
)
(for /l %%a in (-%Height% 17 %EndLine%) do (
set "ubb="
for /l %%b in (-%Width% 4 %Width%) do (
set "fontcolor="
set /a r2=%%a*%%a+%%b*%%b-n2*n2
if !r2! geq !rMin! if !r2! leq !rMax! (
for /l %%z in (0 1 !Count!) do (
if !r[%%z]! leq !r2! set nnn=%%z&set "fontcolor=!RGB[%%z]!"
)
)
if not defined fontcolor set "fontcolor=white"
set "ubb=!ubb![color=!fontcolor!].[/color]"
)
echo;!ubb!
)) >ubb.txt
pauseCOPY
2

评分人数

TOP

回复 2# 523066680


    http://www.bathome.net/thread-36622-1-1.html **疗法。
下载安装python3 https://www.python.org/downloads/ 代码存为xx.py 双击运行或IDLE打开F5运行

TOP

本帖最后由 aa77dd@163.com 于 2015-12-25 04:15 编辑

回复 3# CrLf

大冬天的, 木太阳, 更难看到彩虹啊



.................................................................................................................................................................
.................................................................................................................................................................................
..........................................................................................................................................................................................
....................................................................................................................................................................................................
.........................................................................................................................................................................................................
................................................................................................................................................................................................................
.....................................................................................................................................................................................................................
...........................................................................................................................................................................................................................
...............................................................................................................................................................................................................................
...................................................................................................................................................................................................................................
.......................................................................................................................................................................................................................................
...........................................................................................................................................................................................................................................
..............................................................................................................................................................................................................................................
.................................................................................................................................................................................................................................................
....................................................................................................................................................................................................................................................
.......................................................................................................................................................................................................................................................
.........................................................................................................................................................................................................................................................
............................................................................................................................................................................................................................................................
.............................................................................................................................................................................................................................................................




无聊弄一 cmd 生成 Discuz 彩虹代码
@echo off & title discuz_RAINBOW_CODE_gen_彩虹代码生成器.cmd
setlocal enableDelayedExpansion
set "pnt="
set /a "sum=0"
for %%a in (00 2A 54 7E A8 D2 FC) do (
    if not "!pnt!"=="" (
        set "P!pnt!=%%a"
        set "N%%a=!pnt!"
    )
    set "pnt=%%a"
    set /a "sum+=1"
)
set /a "sumColor = (sum - 1) * 5 + 1"
set        "R=FC"
set        "G=00"
set        "B=00"
set       "co=RGB"
set  "ALLMODE=PN"
set     "mode=P"
set    "bound=PFCN00"
set   "act_ci=1"
set   "act_ci=2"
set        "B=2A"
set     "mode=N"
REM 行,列间距比
set "r_R2C=14 / 4"
set "r_C2R=4 / 14"
set /a "top_compensation = 0"
set /a "thick = sumColor * %r_R2C%, rad = thick * 2 / 8, t = (rad_max = thick + rad) + 1, r_sq_max = t * t + top_compensation"
set /a "margin = 2, wid = (rad_max + margin) * 2, hei = rad_max * 1 / 2, L_hei = hei * %r_C2R%, xc = wid / 2, yc = rad_max + margin"
>discuz_RAINBOW_CODE_彩虹代码.txt cd.
set /a "radius=rad_max"
for /L %%i in (1 1 %sumColor%) do (
    for %%j in (!act_ci!) do set "act_c=!co:~%%j,1!"
    for %%c in (!act_c!)  do set "cv=!%%c!"
    for %%m in (!mode!!cv!) do if /i not "!bound:%%m=!"=="%bound%" (
        for %%n in (!mode!) do set "mode=!ALLMODE:%%n=!"
        set /a "act_ci = (act_ci - 1) %% 3"
        for %%j in (!act_ci!) do set "act_c=!co:~%%j,1!"
        for %%c in (!act_c!) do set "cv=!%%c!"
    )
    for /f "tokens=1-2" %%a in ("!mode! !cv!") do set "!act_c!=!%%a%%b!"
    set /a "@%%i = r_sq_min = radius * radius, radius -= 1"
    if %%i equ 15 set /a "@15 += 0" & rem 紫区补偿
    set "C_!@%%i!=!R!!G!!B!"
)
set r_sq
set C_
set @
set "exp=0"
for /L %%i in (%sumColor% -1 1) do (
    set "exp=t=x-@%%i>>31,~t&@%%i|t&(!exp!)"
)
set exp
for /L %%y in (0 1 !L_hei!) do (
    set "wh="
    for /L %%x in (0 1 !wid!) do (
        set /a "dx = %%x - xc, dy = (%%y * %r_R2C%) - yc, sq_d = dx * dx + dy * dy"
        set "c="
        if !sq_d! lss !r_sq_min! (
            set "c=white"
        ) else if !sq_d! geq !r_sq_max! (
            set "c=white"
        )
        if not "!c!"=="white" (
            set /a "x=sq_d,cpnt=(%exp%)"
            for %%p in (!cpnt!) do set "c=#!C_%%p!"
            if not "!wh!"=="" (
                >>discuz_RAINBOW_CODE_彩虹代码.txt <nul set /p "=[color=white]!wh![/color]"
            )
            >>discuz_RAINBOW_CODE_彩虹代码.txt <nul set /p "=[color=!c!].[/color]"
            REM >>discuz_RAINBOW_CODE_彩虹代码.txt <nul set /p "=!sq_d!/!c! "
            set "wh="
        ) else (
            set "wh=!wh!."
        )
    )
    REM if not "!wh!"=="" (
        REM 最后的空白可省略
        REM >>discuz_RAINBOW_CODE_彩虹代码.txt <nul set /p "=[color=white]!wh![/color]"
    REM )
    >>discuz_RAINBOW_CODE_彩虹代码.txt echo;
)
start "" discuz_RAINBOW_CODE_彩虹代码.txt
pause
exitCOPY
2

评分人数

TOP

回复 3# CrLf


    噢,谢特。
[url=][/url]

TOP

回复 2# 523066680


    CrLf 只是缩写,其实我的全名是:
NulSohStxEtxEotEnqAckBelBsHtLfVtFfCrSoSiDleDc1Dc2Dc3Dc4NakSynEtbCanEmSubEscFsGsRsUs

TOP

难怪ID 叫 CrLf
[url=][/url]

TOP

返回列表