找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 19174|回复: 2

[文本处理] [已解决]根据文本指定内容进行指定排序?

[复制链接]
发表于 2022-5-14 12:40:20 | 显示全部楼层 |阅读模式
本帖最后由 czvde 于 2022-5-15 19:54 编辑

文件夹有以下文件:
123.txt
23.txt
……
3000.txt

123.txt的内容如下:
aa_aa
123_123
100_900_001


效果
123-1.txt的内容如下:
aa_aa
100_900_001
123_123

123-a.txt的内容如下:
123_123
100_900_001
aa_aa

123-z.txt的内容如下:
100_900_001
123_123
aa_aa

已解决!
发表于 2022-5-14 17:16:37 | 显示全部楼层
  1. #@&cls&powershell "type %~s0|out-string|iex"&pause&exit
  2. $a=@{}
  3. gc '123.txt'|%{$a[$_.split('_')[1].length]+=@($_)}
  4. sc '123-1.txt' $($a.keys|sort{[int]$_}|%{$a[$_]|sort -desc{$_.split('_')[1]}})
  5. sc '123-a.txt' $($a.keys|%{$a[$_]|sort{$_.split('_')[1]}})
  6. sc '123-z.txt' $($a.keys|sort -desc{[int]$_}|%{$a[$_]|sort -desc{$_.split('_')[1]}})
复制代码

评分

参与人数 1技术 +1 收起 理由
czvde + 1 谢谢

查看全部评分

发表于 2022-5-14 18:51:10 | 显示全部楼层
  1. <#*,:&cls
  2. @echo off
  3. cd /d "%~dp0"
  4. powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath "%~f0" -ReadCount 0 | Out-String)))"
  5. pause
  6. exit /b
  7. #>

  8. $arrFiles = "123.txt", "23.txt", "3000.txt"
  9. foreach ($itemFile in $arrFiles) {
  10.   try {
  11.     $basename = [system.io.path]::GetFileNameWithoutExtension($itemFile)
  12.     $ext = [System.IO.Path]::GetExtension($itemFile)
  13.     $psoList = (Get-Content -ReadCount 0 -LiteralPath $itemFile -ErrorAction Stop) | Select-Object -Property @{
  14.       Name       = 'Line'
  15.       Expression = { $_ }
  16.     }, @{
  17.       Name       = 'Field2'
  18.       Expression = { ($_ -split '_')[1] }
  19.     }
  20.     $psoList | Sort-Object -Property @{
  21.       Expression = {
  22.         $_.Field2.Length
  23.       }
  24.       Ascending  = $true
  25.     }, @{
  26.       Expression = {
  27.         $_.Field2
  28.       }
  29.       Ascending  = $false
  30.     } | Select-Object -ExpandProperty Line | Set-Content -LiteralPath "${basename}-1${ext}"
  31.     $psoList | Sort-Object -Property @{
  32.       Expression = {
  33.         $_.Field2.Length
  34.       }
  35.       Ascending  = $false
  36.     }, @{
  37.       Expression = {
  38.         $_.Field2
  39.       }
  40.       Ascending  = $true
  41.     } | Select-Object -ExpandProperty Line | Set-Content -LiteralPath "${basename}-a${ext}"
  42.     $psoList | Sort-Object -Property @{
  43.       Expression = {
  44.         $_.Field2.Length
  45.       }
  46.       Ascending  = $false
  47.     }, @{
  48.       Expression = {
  49.         $_.Field2
  50.       }
  51.       Ascending  = $false
  52.     } | Select-Object -ExpandProperty Line | Set-Content -LiteralPath "${basename}-z${ext}"

  53.   } catch {}
  54. }
复制代码

评分

参与人数 1技术 +1 收起 理由
czvde + 1 谢谢

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-19 23:34 , Processed in 0.017210 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表