Board logo

标题: [文本处理] [已解决]求助一个批处理,只取出每天的最早和最晚的时间 [打印本页]

作者: laolong    时间: 2023-2-22 17:20     标题: [已解决]求助一个批处理,只取出每天的最早和最晚的时间

本帖最后由 laolong 于 2023-2-23 12:25 编辑

文本文件 text.txt 中保存有如下格式的数据:
  1. 2022-12-01 08:50:38
  2. 2022-12-01 10:52:47
  3. 2022-12-01 13:01:03
  4. 2022-12-01 13:58:14
  5. 2022-12-01 18:33:11
  6. 2022-12-01 18:56:31
  7. 2022-12-01 19:32:00
  8. 2022-12-01 19:32:01
  9. 2022-12-02 08:54:28
  10. 2022-12-02 09:26:54
  11. 2022-12-02 10:15:47
  12. 2022-12-02 13:59:48
  13. 2022-12-02 17:24:50
  14. 2022-12-02 18:03:51
  15. 2022-12-05 09:10:37
  16. 2022-12-05 11:09:49
  17. 2022-12-05 12:59:48
  18. 2022-12-05 13:41:24
  19. 2022-12-05 15:23:46
  20. 2022-12-05 16:01:26
  21. 2022-12-05 18:05:07
  22. 2022-12-06 09:10:31
  23. 2022-12-06 09:47:02
  24. 2022-12-06 11:08:03
  25. 2022-12-06 16:06:40
  26. 2022-12-06 18:18:23
复制代码
想获取每天的第一个(最早)和最后一个(最晚)的记录到一个文本文件中,两个记录之间以 tab 隔开:
  1. 2022-12-01 08:50:38 2022-12-01 19:32:01
  2. 2022-12-02 08:54:28 2022-12-02 18:03:51
  3. 2022-12-05 09:10:37 2022-12-05 18:05:07
  4. 2022-12-06 09:10:31 2022-12-06 18:18:23
复制代码
先谢谢了
作者: idwma    时间: 2023-2-22 17:48

  1. #@&cls&powershell "type %~s0|out-string|iex"&pause&exit
  2. $a=@{}
  3. gc text.txt|%{
  4.     $b=$_ -split ' '
  5.     $a[$b[0]]+=,$b[1]
  6. }
  7. $a.keys|sort|%{
  8.     $b=$a[$_]|sort
  9.     "{0} {1}`t{0} {2}" -f $_,$b[0],$b[-1]
  10. }|sc txt.txt
复制代码

作者: laolong    时间: 2023-2-22 17:54

回复 2# idwma


    厉害厉害!强啊!  多谢多谢
作者: laolong    时间: 2023-2-22 18:03

回复 2# idwma


    我看不懂代码,所以也无从下手修改。能否将输出格式改一下:日期 tab 最早的时间 tab 最晚的时间,这样可以直接导入 excel 来处理了,谢谢谢谢
  1. 2022-12-01 08:50:38 19:32:01
  2. 2022-12-02 08:54:28 18:03:51
  3. 2022-12-05 09:10:37 18:05:07
  4. 2022-12-06 09:10:31 18:18:23
复制代码

作者: laolong    时间: 2023-2-22 18:11

倒数第二行尝试修改为
  1.     "{0}`t{1}`t{2}" -f $_,$b[0],$b[-1]
复制代码
完美了,之前我考虑不周,这下省事多了

再次感谢大侠相助
作者: terse    时间: 2023-2-22 20:49

  1. <# : PS
  2. @echo off
  3. powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"> t.txt
  4. pause & exit
  5. #>
  6. gc text.txt|sort|group{ $_.split('')[0]}|%{$($_.group[0]+"`t"+$_.group[-1]) -replace "\s","`t"}
复制代码

作者: qixiaobin0715    时间: 2023-2-23 08:37

本帖最后由 qixiaobin0715 于 2023-2-23 08:53 编辑
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. (for /f "tokens=1,2" %%i in (text.txt) do (
  4.     if %%i neq !str1! (
  5.         if defined str1 (
  6.             echo,!str1! !str2! !str3!
  7.         )
  8.         set str1=%%i
  9.         set str2=%%j
  10.     ) else (
  11.         set str3=%%j
  12.     )
  13. )
  14. echo,!str1! !str2! !str3!)>1.txt
  15. pause
复制代码

作者: hfxiang    时间: 2023-2-23 09:00

回复 1# laolong

用第3方工具gawk( http://bcn.bathome.net/tool/4.1.3/gawk.exe )的实现方法如下:
  1. gawk "{a[$1]?(a[$1]>$2?a[$1]=$2:0):(a[$1]=$2);b[$1]?(b[$1]<$2?b[$1]=$2:0):(b[$1]=$2)}END{l=asorti(a,c);for(i=1;i<=l;i++){print c[i],a[c[i]]\"\t\"c[i],b[c[i]]}}" text.txt>result.txt
复制代码

作者: laolong    时间: 2023-2-23 10:27

回复 6# terse


    多谢多谢,可以完成
作者: laolong    时间: 2023-2-23 10:28

回复 7# qixiaobin0715


    多谢多谢,可以完成
作者: laolong    时间: 2023-2-23 10:29

回复 8# hfxiang


    多谢多谢,也可以完成




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