[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] [已解决]批处理怎样提取两字符串间内容并剪切对应行?

例如a.txt内容
http://www.tiffany.com/Shopping/item.aspx?sku=11246699
http://www.tiffany.com/Shopping/item.aspx?sku=10907144
http://www.tiffany.com/
http://www.tiffany.com/Shared/scripts/AC_OETags.js?v=5.0
http://www.tiffany.com/Shared/scripts/flashAPI.js?v=5.0
http://www.tiffany.com/Customer/WishList/WishList.aspx?sku=12323435&cid=622067&grp=GRP02969&mcat=148204
http://www.tiffany.com/item.aspx?sku=23712318&cid=287461&search_params=s+5-p+1-c+287461-r+-x+-n+6-ri+-ni+1-t&mcat=148204

接受管理员   尘土飞扬 的意见重新编辑问题,为避免错误,分两步进行

第一步
提取提取每行中含 http://www.tiffany.com/Shopping/item.aspx?sku=字符(不是每行中都含有这个字符串)  这行中这个字符串后面的内容到c.txt
并将提取过内容的行剪切到b.txt
处理后
a.txt
http://www.tiffany.com/
http://www.tiffany.com/Shared/scripts/AC_OETags.js?v=5.0
http://www.tiffany.com/Shared/scripts/flashAPI.js?v=5.0
http://www.tiffany.com/Customer/WishList/WishList.aspx?sku=12323435&cid=622067&grp=GRP02969&mcat=148204
http://www.tiffany.com/item.aspx?sku=23712318&cid=287461&search_params=s+5-p+1-c+287461-r+-x+-n+6-ri+-ni+1-t&mcat=148204

b.txt
http://www.tiffany.com/Shopping/item.aspx?sku=11246699
http://www.tiffany.com/Shopping/item.aspx?sku=10907144

c.txt
11246699
10907144

第二步
提取每行sku&间的内容(不是每行都含有sku 和&),到e.txt
并将提取过内容的行剪切到d.txt
处理后
a.txt
http://www.tiffany.com/
http://www.tiffany.com/Shared/scripts/AC_OETags.js?v=5.0
http://www.tiffany.com/Shared/scripts/flashAPI.js?v=5.0

d.txt
http://www.tiffany.com/Customer/WishList/WishList.aspx?sku=12323435&cid=622067&grp=GRP02969&mcat=148204
http://www.tiffany.com/item.aspx?sku=23712318&cid=287461&search_params=s+5-p+1-c+287461-r+-x+-n+6-ri+-ni+1-t&mcat=148204

e.txt
12323435
23712318

[ 本帖最后由 daohe 于 2010-4-5 22:37 编辑 ]
1

评分人数

    • Batcher: 感谢主动给标题标注[已解决]字样PB + 2

  1. sed -n "/http:\/\/www.tiffany.com\/Shopping\/item.aspx?sku=/p" a.txt >b.txt
  2. sed "/sku=/!d;s/.*sku=//g;s/&.*//g" a.txt >c.txt
  3. sed -i "/sku=/d" a.txt
复制代码

TOP

  1、请精确描述a.txt中数据的组成规律,比如说:同一行上,是不是 sku= 和 & 一定同时存在?
  2、请再次检查顶楼的描述,看看是否有容易让别人莫名其妙或产生歧义之处。比如:“提取sku=和&之间的内容, http://www.tiffany.com/Shopping/item.aspx?sku=之后的内容到c.txt”这一句,究竟是少了句子成分,还是颠倒了语序?
  3、之所以在这里大谈语法问题、死钻字眼,是因为细节虽小,但是这关系到写出的代码是不是白忙活一场的严肃问题,需要认真对待——没有人是任劳任怨的活雷锋,要想早日解决问题,请先为帮你的人考虑一下下。
尺有所短寸有所长,学好批处理没商量;
考虑问题复杂化,解决问题简洁化。

心在天山,身老沧州。

TOP

问题已修正,二楼测试不成功!

TOP

二楼测试很成功
C:\Test>type a.txt
http://www.tiffany.com/Shopping/item.aspx?sku=11246699
http://www.tiffany.com/Shopping/item.aspx?sku=10907144
http://www.tiffany.com/
http://www.tiffany.com/Shared/scripts/AC_OETags.js?v=5.0
http://www.tiffany.com/Shared/scripts/flashAPI.js?v=5.0
http://www.tiffany.com/Customer/WishList/WishList.aspx?sku=12323435&cid=622067&grp=GRP02969&mcat=148204
http://www.tiffany.com/item.aspx?sku=23712318&cid=287461&search_params=s+5-p+1-c+287461-r+-x+-n+6-ri+-ni+1-t&mcat=148204

C:\Test>sed -n "/http:\/\/www.tiffany.com\/Shopping\/item.aspx?sku=/p" a.txt >b.txt

C:\Test>sed "/sku=/!d;s/.*sku=//g;s/&.*//g" a.txt >c.txt

C:\Test>sed -i "/sku=/d" a.txt

C:\Test>type b.txt
http://www.tiffany.com/Shopping/item.aspx?sku=11246699
http://www.tiffany.com/Shopping/item.aspx?sku=10907144

C:\Test>type c.txt
11246699
10907144
12323435
23712318

C:\Test>type a.txt
http://www.tiffany.com/
http://www.tiffany.com/Shared/scripts/AC_OETags.js?v=5.0
http://www.tiffany.com/Shared/scripts/flashAPI.js?v=5.0

TOP

我测试怎么不行

http://bbs.bathome.net/thread-1114-1-1.html
上的两个sed版本我都用了(是直接双击sed.exe运行后,在运行批处理就oK了吧),还是不行

TOP

第一步
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "str=http://www.tiffany.com/Shopping/item.aspx?sku="
  4. findstr "%str%" a.txt >>b.txt
  5. for /f "tokens=2 delims==" %%i in (b.txt) do (
  6.   echo %%i >>c.txt
  7.            )
  8. pause
复制代码

第二步的&我暂时处理不来,晚点再继续测试

[ 本帖最后由 sgaizxt001 于 2010-4-5 01:05 编辑 ]

TOP

回复 7楼 的帖子

TOP

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "str=http://www.tiffany.com/Shopping/item.aspx?sku="
  4. findstr "%str%" a.txt >>b.txt
  5. findstr /v "%str%" a.txt >>f.txt
  6. for /f "tokens=2 delims==" %%i in (b.txt) do (
  7.   echo %%i >>c.txt
  8.            )
  9. for /f "tokens=1,2,3 delims==" %%a in (f.txt) do (
  10.   if not "%%c" == "" (set str=%%b & set str=!str:^&cid=! & call echo %%str%% >>e.txt)
  11. )
  12. ping 127.1 -n 4 >nul
  13. del a.txt
  14. ren f.txt a.txt
  15. pause
复制代码
暂时可以生成a,b,c,e.txt。.d.txt也没生成,不过应该不难,而且我这个代码漏洞很大,只是根据你给的这几个来投机取巧弄的,还是用sed的那个好,不过我对sed不懂,看版主的没问题的话应该是没问题的

[ 本帖最后由 sgaizxt001 于 2010-4-5 06:57 编辑 ]
1

评分人数

TOP

回复 6楼 的帖子

1、建议使用4.14的那个版本
2、不用双击sed.exe
3、把sed.exe放在c:\Windows\System32\目录下面或者当前目录下面

TOP

回复 2楼 的帖子

测试结果正确!!!!

TOP

返回列表