Board logo

标题: [文本处理] [已解决]批处理如何将txt文本按每行行尾的数字大小排序? [打印本页]

作者: zwgxsy    时间: 2011-8-10 09:27     标题: [已解决]批处理如何将txt文本按每行行尾的数字大小排序?

本帖最后由 pcl_test 于 2016-7-12 17:00 编辑

比如:
http://www.baidu.com/  排名:6
http://www.google.com/ 排名:1
http://www.bathome.net/ 排名:340977

结果:
http://www.google.com/ 排名:1
http://www.baidu.com/  排名:6
http://www.bathome.net/ 排名:340977

求解
作者: CrLf    时间: 2011-8-10 10:35

  1. @echo off&setlocal enabledelayedexpansion
  2. %1 for /f "tokens=1* delims=:" %%a in ('%0 ::^|sort') do echo %%b
  3. %1 pause&exit
  4. for /f "tokens=1* delims=:" %%a in (1.txt) do (
  5. set n=       %%b
  6. echo !n:~-6!:%%a:%%b
  7. )
复制代码

作者: ArdentMan    时间: 2011-8-10 10:49

赞!
用::来做参数起到第二次注释二三句的作用
参数真是
用神了
神了

作者: zwgxsy    时间: 2011-8-10 16:53

@echo off&setlocal enabledelayedexpansion
%1 for /f "tokens=1* delims=:" %%a in ('%0 ::^|sort') do echo %%b
%1 pause&exit
for /f "tokens=1* delims=:" %%a in (11.txt) do (
        set n=       %%b
        echo !n:~-6!:%%a:%%b >>2.txt
)
结果怎么是
       1:http://www.google.com/ 排名:1
  340977:http://www.bathome.net/ 排名:340977
       6:http://www.baidu.com/  排名:6
作者: CrLf    时间: 2011-8-10 16:57

输出最终结果的是第一个 for,而不是第二个 for
作者: zwgxsy    时间: 2011-8-10 17:01

确实有点晕了,不过问题解决了,谢谢。
作者: pcl_test    时间: 2016-7-12 17:08

  1. //&cls&dir /a-d/b 1.txt|cscript -nologo -e:jscript "%~f0"&pause&exit
  2. var fso = new ActiveXObject('Scripting.Filesystemobject');
  3. function SortNumber(file){
  4.     var f = fso.OpenTextFile(file, 1);
  5.     try{
  6.         var txt = f.ReadAll().split(/\r\n/).sort(
  7.             function(a, b){return Number(a.replace(/.*(\d+)\s*$/, '$1'))-Number(b.replace(/.*(\d+)\s*$/, '$1'))}
  8.         ).join('\r\n');
  9.         f.Close();
  10.         fso.OpenTextFile(file, 2).Write(txt);
  11.     }catch(e){}
  12. }
  13. while(!WSH.StdIn.AtEndOfStream)SortNumber(WSH.StdIn.ReadLine());
  14. WSH.echo('Done');
复制代码





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