Board logo

标题: [文本处理] 请教,批处理提取大于指定值的行 [打印本页]

作者: keshun    时间: 2019-3-7 12:20     标题: 请教,批处理提取大于指定值的行

本帖最后由 keshun 于 2019-3-7 13:32 编辑

原文1.txt
1304.280 2,336.4485
1324.290 5,503.4965
1314.284 4,380.9523
1384.319 3,998.8423
1394.321 2,314.2791
1364.314 11,494.6685
1544.420 1,144.7963
1764.500 2,673.3333
12:03:14        3.825        2,596.0784
12:03:10        3.831        1,743.67
12:03:09        3.833        2,363.6837
12:03:06        3.833        1,228.8025

输出到2.txt
1324.290 5,503.4965
1364.314 11,494.6685
12:03:14        3.825        2,596.0784
12:03:09        3.833        2,363.6837

要求第1段两组数字串的行有大于“5,”的行输出,第2段三组数字串的行有大于“2,”的行输出。
请教高手求代码,谢谢!!!
作者: yhcfsr    时间: 2019-3-7 13:20

  1. $text = [io.file]::ReadAllLines('1.txt', [text.encoding]::Default);
  2. foreach($line in $text)
  3. {
  4.     if ($line -notmatch ':') {
  5.         if( ($line -match '\s+(\d+),') -and ([int]$Matches[1] -ge 5) ) {$line}
  6.     }
  7.     else {
  8.         if( ($line -match '\s+(\d+),') -and ([int]$Matches[1] -ge 2) ) {$line}
  9.     }
  10. }
复制代码

作者: keshun    时间: 2019-3-7 13:30

本帖最后由 keshun 于 2019-3-7 13:34 编辑

谢谢2楼,代码我试过会出错(win7 32系统),有点看不明白
作者: yhcfsr    时间: 2019-3-7 13:40

回复 3# keshun


    这是powershell脚本,使用方法请百度.也可忽略.
作者: tigerpower    时间: 2019-3-7 15:25

  1. powershell -c "gc 1.txt|?{$_ -match '(?m)^\S+\s+((\d\d|[5-9]),\S+$|\S+\s+(\d\d|[2-9]),\S+$)'}
复制代码





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