标题: [问题求助] [已解决]请教PowerShell怎样匹配替换有特定规律的文本 [打印本页]
作者: smss 时间: 2022-10-15 00:15 标题: [已解决]请教PowerShell怎样匹配替换有特定规律的文本
本帖最后由 smss 于 2022-10-15 23:24 编辑
- 2501 GA106 [GeForce RTX 3060]
- 2503 GA106 [GeForce RTX 3060]
- 2504 GA106 [GeForce RTX 3060 Lite Hash Rate]
- 2505 GA106
- 2507 GA106 [Geforce RTX 3050]
- 2508 GA106 [GeForce RTX 3050 OEM]
- 2520 GA106M [GeForce RTX 3060 Mobile / Max-Q]
- 2523 GA106M [GeForce RTX 3050 Ti Mobile / Max-Q]
- 252f GA106 [GeForce RTX 3060 Engineering Sample]
- 2531 GA106 [RTX A2000]
- 2560 GA106M [GeForce RTX 3060 Mobile / Max-Q]
- 2563 GA106M [GeForce RTX 3050 Ti Mobile / Max-Q]
- 2571 GA106 [RTX A2000 12GB]
- 2583 GA107 [GeForce RTX 3050]
- 25a0 GA107M [GeForce RTX 3050 Ti Mobile]
- 25a2 GA107M [GeForce RTX 3050 Mobile]
- 25a3 GA107
- 25a4 GA107
- 25a5 GA107M [GeForce RTX 3050 Mobile]
复制代码
[Geforce RTX 3050]替换为[GeForce RTX 3050 4G]
[GeForce RTX 3050 Ti Mobile]替换为[GeForce RTX 3050 Ti Mobile 4G]
...
同理
[GeForce RTX 3060]替换为[GeForce RTX 3060 6G]
[GeForce RTX 3060 Lite Hash Rate] 替换为[GeForce RTX 3060 Lite Hash Rate 6G]
...
还有
3070
3080
很多条
最好备注下 因为看不懂
抛砖引玉- param([strinG]$file); #修改前的文件
- $file1 = [io.fileinfo]$file;
- $file2 = ".\1.txt"
- $UTF8NoBom = New-Object System.Text.UTF8Encoding $false;
- $str = [IO.File]::ReadAllText($file, [Text.Encoding]::Default);
- $str = $str -replace'3050]','3050 4G]'-replace'3060]','3060 6G]'-replace '\r\n', "`n";
- [IO.File]::WriteAllText($file2, $str, $UTF8NoBom);
复制代码
作者: WHY 时间: 2022-10-15 14:21
本帖最后由 WHY 于 2022-10-15 22:41 编辑
- param([string]$file); #修改前的文件
- $file2 = ".\1.txt"; #修改后的文件
-
- $Hash1 = @{ '5' = '4G'; '6' = '6G'; '7' = '8G'; '8' = '8G' }
- $Hash2 = @{ '5' = '4G'; '6' = '6G'; '7' = '8G'; '8' = '16G'; '9' = '24G' }
- $Block1 = {
- param($m);
- $m0 = $m.Groups[0].Value;
- $m1 = $m.Groups[1].Value;
- if( $m0 -match '\d+GB?' ){
- return $m0 -replace '(\d+G)B', '$1';
- } else {
- return $m0 + ' ' + $Hash1[$m1];
- }
- }
-
- $Block2 = {
- param($m);
- $m0 = $m.Groups[0].Value;
- $m1 = $m.Groups[1].Value;
- if( $m0 -match '\d+GB?' ){
- return $m0 -replace '(\d+G)B', '$1';
- } else {
- return $m0 + ' ' + $Hash2[$m1];
- }
- }
-
- $UTF8NoBom = New-Object System.Text.UTF8Encoding $false;
- $str = [IO.File]::ReadAllText($file, [Text.Encoding]::UTF8);
- $str = [regex]::Replace( $str, '\b[12]0([5-8])0[^[\]]*(?=])', $Block1 );
- $str = [regex]::Replace( $str, '\b[34]0([5-9])0[^[\]]*(?=])', $Block2 );
- #$str = $str -replace '\r\n', "`n";
- [IO.File]::WriteAllText($file2, $str, $UTF8NoBom);
-
- echo 'Done';
- [Console]::ReadLine();
复制代码
作者: smss 时间: 2022-10-15 20:17
本帖最后由 smss 于 2022-10-15 21:26 编辑
回复 2# WHY
老师
如果文本中含有GB就只把GB替换为G 而不添加G怎样改
这是要修改的文本->https://share.weiyun.com/AXZ1xaz5
249c GA104M [GeForce RTX 3080 Mobile / Max-Q 8GB/16GB]修改为249c GA104M [GeForce RTX 3080 Mobile / Max-Q 8G/16G]
我这照葫芦画瓢 知其然不知所以然的怕是要丢人了- param([string]$file); #修改前的文件
- $file2 = ".\1.txt"; #修改后的文件
-
- $Hash1 = @{ '5' = '4G'; '6' = '6G'; '7' = '8G'; '8' = '8G'}
- $Hash2 = @{ '5' = '4G'; '6' = '6G'; '7' = '8G'; '8' = '16G'; '9' = '24G' } #3050=4G 3060=6G 3070=8G 3080=10G
- $UTF8NoBom = New-Object System.Text.UTF8Encoding $false;
-
- $str = [IO.File]::ReadAllText($file, [Text.Encoding]::Default);
- $str = [regex]::Replace( $str, '\b10([5-8])0[^\]]*(?=])', { param($m); $m.Groups[0].Value + ' ' + $Hash1[$m.Groups[1].Value] } );
- $str = [regex]::Replace( $str, '\b20([5-8])0[^\]]*(?=])', { param($m); $m.Groups[0].Value + ' ' + $Hash1[$m.Groups[1].Value] } );
- $str = [regex]::Replace( $str, '\b30([5-9])0[^\]]*(?=])', { param($m); $m.Groups[0].Value + ' ' + $Hash2[$m.Groups[1].Value] } );
- $str = [regex]::Replace( $str, '\b40([5-9])0[^\]]*(?=])', { param($m); $m.Groups[0].Value + ' ' + $Hash2[$m.Groups[1].Value] } );
- $str = $str -replace '\r\n', "`n";
- [IO.File]::WriteAllText($file2, $str, $UTF8NoBom);
-
- echo 'Done';
- [Console]::ReadLine();
复制代码
148c 2357 Grenada XT [Radeon R9 390X] 也会被替换为148c 2357 Grenada XT [Radeon R9 390X 8G]
作者: WHY 时间: 2022-10-15 22:41
回复 3# smss
已修改
作者: smss 时间: 2022-10-16 08:51
回复 4# WHY WHY老师 总是能求同存异让代码很精减 膜拜老师
作者: smss 时间: 2023-3-4 15:02
本帖最后由 smss 于 2023-3-5 18:38 编辑
回复 2# WHY
40系列发布了细看下错了不少 望修正 拜托 此处下载https://pci-ids.ucw.cz/笔电有Laptop GPU
例子:
2684 AD102 [GeForce RTX 4090] 替换为 2684 AD102 [GeForce RTX 4090 24G]
2717 GN21-X11 [GeForce RTX 4090 Laptop GPU] 2717 GN21-X11 [GeForce RTX 4090 Laptop GPU 16G]
桌面和笔电显存容量并不相同祥见下表 空白表示没有- RTX 10系列 桌面显存 笔电显存
- GTX 1080 Ti 8GB 8GB
- GTX 1080 8GB 8GB
- GTX 1070 8GB 8GB
- GTX 1060 6GB 6GB
- GTX 1050 Ti 4GB 4GB
- GTX 1050 2GB 2GB
-
- RTX 16系列
- GTX 1660 Ti 6GB 6GB
- GTX 1660 Super 6GB
- GTX 1660 6GB
- GTX 1650 Super 4GB
- GTX 1650 (G5) 4GB
- GTX 1650 (G6) 4GB
- GTX 1630 4GB
- GTX 1650 Ti 4GB 4GB
- GTX 1650 4GB 4GB
-
- RTX 20系列
- RTX 2080 Ti 11 GB
- RTX 2080 SUPER 8 GB 8 GB
- RTX 2080 8 GB 8 GB
- RTX 2070 SUPER 8 GB 8 GB
- RTX 2070 8 GB 8 GB
- RTX 2060 SUPER 8 GB 8 GB
- RTX 2060 6 GB 6 GB
- RTX 2050 4 GB 4 GB
-
- RTX 30系列
- RTX 3090 Ti 24 GB
- RTX 3090 24 GB
- RTX 3080 Ti 12 GB 16 GB
- RTX 3080 12 GB / 10 GB 8 GB / 16 GB
- RTX 3070 Ti 8 GB 8 GB
- RTX 3070 8 GB 8 GB
- RTX 3060 Ti 8 GB
- RTX 3060 12 GB 6 GB
- RTX 3050 Ti 6 GB 4 GB
- RTX 3050 8 GB 4 GB
-
- RTX 40系列
- RTX 4090 24 GB 16 GB
- RTX 4080 16 GB 12 GB
- RTX 4070 Ti 12 GB
- RTX 4070 12 GB 8 GB
- RTX 4060 Ti 8GB
- RTX 4060 8 GB? 8 GB
- RTX 4050 6 GB? 6 GB
复制代码
作者: WHY 时间: 2023-3-4 18:22
回复 6# smss - $Block2 = {
- param($m);
- $m0 = $m.Groups[0].Value;
- $m1 = $m.Groups[1].Value;
- If( $m0 -match '\d+GB?'){
- return $m0 -replace '(\d+G)B', '$1';
- } elseIf ($m0 -match '\b4090 Laptop GPU'){
- return $m0 + ' 16G';
- } else {
- return $m0 + ' ' + $Hash2[$m1];
- }
- }
复制代码
作者: newswan 时间: 2023-3-4 21:41
本帖最后由 newswan 于 2023-3-4 21:44 编辑
建议- $hash = @{
- '3050' = ' 4G'
- '3060' = ' 6G'
- '3070' = ' 8G'
- }
-
- $block = {
- param($m)
- $m0 = $m.Groups[0].Value
- $m1 = $m.Groups[1].Value
- return $m0 + $hash[$m1]
- }
-
- $str = [regex]::Replace( $str, 'RTX (\d+) [^]]+', $Block );
复制代码
作者: WHY 时间: 2023-3-6 01:42
本帖最后由 WHY 于 2023-3-6 08:48 编辑
回复 6# smss
这样试下
修改正则模式;增加 1070 Ti,我测试没有问题- $file1 = 'pci.ids'; #修改前的文件
- $file2 = '1.txt'; #修改后的文件
-
- #桌面系列
- $Hash1 = @{
- '1050' = '2G'; #10
- '1050 Ti' = '4G';
- '1060' = '6G';
- '1070' = '8G';
- '1070 Ti' = '8G';
- '1080' = '8G';
- '1080 Ti' = '8G';
- '1650' = '4G'; #16
- '1630' = '4G';
- '1660' = '6G';
- '2050' = '4G'; #20
- '2060' = '6G';
- '2060 SUPER' = '8G';
- '2070' = '8G';
- '2070 SUPER' = '8G';
- '2080' = '8G';
- '2080 SUPER' = '8G';
- '2080 Ti' = '11G';
- '3050' = '8G'; #30
- '3050 Ti' = '6G';
- '3060' = '12G';
- '3060 Ti' = '8G';
- '3070' = '8G';
- '3070 Ti' = '8G';
- '3080' = '12G / 10G';
- '3080 Ti' = '12G';
- '3090' = '24G';
- '3090 Ti' = '24G';
- '4050' = '6G'; #40
- '4060' = '8G';
- '4060 Ti' = '8G';
- '4070' = '12G';
- '4070 Ti' = '12G';
- '4080' = '16G';
- '4090' = '24G'
- }
-
- #移动系列
- $Hash2 = @{
- '1050' = '2G'; #10
- '1050 Ti' = '4G';
- '1060' = '6G';
- '1070' = '8G';
- '1070 Ti' = '8G';
- '1080' = '8G';
- '1080 Ti' = '8G';
- '1650' = '4G'; #16
- '1660' = '6G';
- '2050' = '4G'; #20
- '2060' = '6G';
- '2060 SUPER' = '8G';
- '2070' = '8G';
- '2070 SUPER' = '8G';
- '2080' = '8G';
- '2080 SUPER' = '8G';
- '3050' = '4G'; #30
- '3050 Ti' = '4G';
- '3060' = '6G';
- '3070' = '8G';
- '3070 Ti' = '8G';
- '3080' = '8G / 16G';
- '3080 Ti' = '16G';
- '4050' = '6G'; #40
- '4060' = '8G';
- '4070' = '8G';
- '4080' = '12G';
- '4090' = '16G'
- }
-
- $Block = {
- param($m);
- $m0 = $m.Groups[0].Value;
- $m1 = $m.Groups[1].Value;
- if( $m0 -match '\b[0-9]+GB?' ){
- return $m0 -replace '\b([0-9]+G)B', '$1';
- } elseIf( $m0 -match '(?-i:Mobile|Laptop)' ){
- return $m0 + ' ' + $Hash2[$m1];
- } else {
- return $m0 + ' ' + $Hash1[$m1];
- }
- }
-
- $UTF8NoBom = New-Object System.Text.UTF8Encoding $false;
- $str = [IO.File]::ReadAllText($file1, [Text.Encoding]::UTF8);
- $str = [regex]::Replace( $str, '(?<=[GR]TX )([1-4]0[5-9]0(?: Ti| SUPER)?|16[356]0)\b[^[\]]*(?=])', $Block );
- $str = $str -replace '(?<!\r)\n', "`r`n";
- [IO.File]::WriteAllText($file2, $str, $UTF8NoBom);
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |