Board logo

标题: [文本处理] [已解决]如何根据内容生成新的内容? [打印本页]

作者: czvde    时间: 2019-9-27 01:37     标题: [已解决]如何根据内容生成新的内容?

本帖最后由 czvde 于 2019-9-27 23:08 编辑

样例
1.txt文件内容以下
ABCDEFFGGZ


2.txt里面的内容是:
新文件_A_ABCD_ABCDEFFGGZ

求助各位大神,谢谢!!
作者: xp3000    时间: 2019-9-27 19:33

  1. /*&cls
  2. @echo off
  3. setlocal EnableDelayedExpansion
  4. for /f "delims=" %%a in ('type 1.txt^|cscript -nologo -e:jscript "%~f0"') do (
  5. echo %%a
  6. )>>2.txt
  7. pause&exit
  8. */
  9. var t='', map={}, files = WScript.StdIn.ReadAll().split('\r\n');
  10. for (var i=0; i<files.length; i++){
  11.     var str = files[i].replace(/([A-Z])([A-Z]{4})([A-Z]+)/,'新文件_$1_$1$2_$1$2$3')
  12.     if(!map[str]){map[str] = true;t+=str+'\r\n';}
  13. }
  14. WSH.echo(t);
复制代码
不知道是不是需要这样的
作者: Gin_Q    时间: 2019-9-27 20:27

本帖最后由 Gin_Q 于 2019-9-27 20:28 编辑

@ECHO OFF
setlocal enabledelayedexpansion
for /f %%a in (old.txt) do (set /a n+=1
set str=%%a
set str1=!str:~,1!
set str2=!str:~,4!
if !n! equ 1 echo _!str1!_!str2!_!str!
if !n! equ 2 echo _!str1!_!str2!_!str!
if !n! equ 3 echo _!str1!_!str2!_!str!
)
pause
作者: Batcher    时间: 2019-9-27 20:29

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. (for /f %%i in ('type "1.txt"') do (
  4.     set str=%%i
  5.     echo 新文件_!str:~0,1!_!str:~0,4!_!str!
  6. ))>"2.txt"
复制代码

作者: czvde    时间: 2019-9-27 21:11

本帖最后由 czvde 于 2019-9-27 23:05 编辑

回复 4# Batcher

您的这个很简洁很好用,请问如果是以下这两种情况,又需要怎样修改才可以实现?

第一种样例
新文件_Z_FGGZ_ABCDEFFGGZ

第二种样例
新文件_Z_ZGGF_ABCDEFFGGZ
作者: Batcher    时间: 2019-9-27 21:40

回复 5# czvde
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. (for /f %%i in ('type "1.txt"') do (
  4.     set str=%%i
  5.     echo 新文件_!str:~-1!_!str:~-4!_!str!
  6. ))>"第一种样例生成效果.txt"
复制代码

作者: terse    时间: 2019-9-27 21:41

回复 5# czvde
字符截取
  1. _!str:~-1!_!str:~-4!
复制代码

作者: Batcher    时间: 2019-9-27 21:41

回复 5# czvde
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. (for /f %%i in ('type "1.txt"') do (
  4.     set str=%%i
  5.     echo 新文件_!str:~-1!_!str:~-1!!str:~-2,1!!str:~-3,1!!str:~-4,1!_!str!
  6. ))>"第二种样例生成效果.txt"
复制代码

作者: Batcher    时间: 2019-9-27 21:42

回复 5# czvde


推荐学习 set 命令视频教程
http://bbs.bathome.net/thread-31727-1-1.html
作者: czvde    时间: 2019-9-27 22:00

本帖最后由 czvde 于 2019-9-27 22:02 编辑

回复 9# Batcher

教程很实用,感谢推荐




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2