标题: [文本处理] [已解决]根据文本指定内容进行指定排序? [打印本页]
作者: czvde 时间: 2022-5-14 12:40 标题: [已解决]根据文本指定内容进行指定排序?
本帖最后由 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
已解决!
作者: idwma 时间: 2022-5-14 17:16
- #@&cls&powershell "type %~s0|out-string|iex"&pause&exit
- $a=@{}
- gc '123.txt'|%{$a[$_.split('_')[1].length]+=@($_)}
- sc '123-1.txt' $($a.keys|sort{[int]$_}|%{$a[$_]|sort -desc{$_.split('_')[1]}})
- sc '123-a.txt' $($a.keys|%{$a[$_]|sort{$_.split('_')[1]}})
- sc '123-z.txt' $($a.keys|sort -desc{[int]$_}|%{$a[$_]|sort -desc{$_.split('_')[1]}})
复制代码
作者: flashercs 时间: 2022-5-14 18:51
- <#*,:&cls
- @echo off
- cd /d "%~dp0"
- powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
- pause
- exit /b
- #>
-
- $arrFiles = "123.txt", "23.txt", "3000.txt"
- foreach ($itemFile in $arrFiles) {
- try {
- $basename = [system.io.path]::GetFileNameWithoutExtension($itemFile)
- $ext = [System.IO.Path]::GetExtension($itemFile)
- $psoList = (Get-Content -ReadCount 0 -LiteralPath $itemFile -ErrorAction Stop) | Select-Object -Property @{
- Name = 'Line'
- Expression = { $_ }
- }, @{
- Name = 'Field2'
- Expression = { ($_ -split '_')[1] }
- }
- $psoList | Sort-Object -Property @{
- Expression = {
- $_.Field2.Length
- }
- Ascending = $true
- }, @{
- Expression = {
- $_.Field2
- }
- Ascending = $false
- } | Select-Object -ExpandProperty Line | Set-Content -LiteralPath "${basename}-1${ext}"
- $psoList | Sort-Object -Property @{
- Expression = {
- $_.Field2.Length
- }
- Ascending = $false
- }, @{
- Expression = {
- $_.Field2
- }
- Ascending = $true
- } | Select-Object -ExpandProperty Line | Set-Content -LiteralPath "${basename}-a${ext}"
- $psoList | Sort-Object -Property @{
- Expression = {
- $_.Field2.Length
- }
- Ascending = $false
- }, @{
- Expression = {
- $_.Field2
- }
- Ascending = $false
- } | Select-Object -ExpandProperty Line | Set-Content -LiteralPath "${basename}-z${ext}"
-
- } catch {}
- }
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |