[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] [已解决]批处理怎样把文本内容分列排序?

本帖最后由 qiulai 于 2022-11-18 19:43 编辑

port.txt内容如下:
IF-MIB::ifDescr.10 = STRING: GigabitEthernet0/0/1
IF-MIB::ifDescr.11 = STRING: GigabitEthernet0/0/2
IF-MIB::ifDescr.12 = STRING: GigabitEthernet0/0/3
IF-MIB::ifDescr.13 = STRING: GigabitEthernet0/0/4
IF-MIB::ifDescr.14 = STRING: GigabitEthernet0/0/5
IF-MIB::ifDescr.15 = STRING: GigabitEthernet0/0/6
IF-MIB::ifDescr.16 = STRING: GigabitEthernet0/0/7
IF-MIB::ifDescr.17 = STRING: GigabitEthernet0/0/8
IF-MIB::ifDescr.18 = STRING: GigabitEthernet0/0/9
IF-MIB::ifDescr.19 = STRING: GigabitEthernet0/0/10
IF-MIB::ifDescr.20 = STRING: GigabitEthernet0/0/11
IF-MIB::ifDescr.21 = STRING: GigabitEthernet0/0/12
IF-MIB::ifDescr.22 = STRING: GigabitEthernet0/0/13
IF-MIB::ifDescr.23 = STRING: GigabitEthernet0/0/14
IF-MIB::ifDescr.24 = STRING: GigabitEthernet0/0/15
IF-MIB::ifDescr.25 = STRING: GigabitEthernet0/0/16
IF-MIB::ifDescr.26 = STRING: GigabitEthernet0/0/17
IF-MIB::ifDescr.27 = STRING: GigabitEthernet0/0/18
IF-MIB::ifDescr.28 = STRING: GigabitEthernet0/0/19
IF-MIB::ifDescr.29 = STRING: GigabitEthernet0/0/20
IF-MIB::ifDescr.30 = STRING: GigabitEthernet0/0/21
IF-MIB::ifDescr.31 = STRING: GigabitEthernet0/0/22
IF-MIB::ifDescr.32 = STRING: GigabitEthernet0/0/23
IF-MIB::ifDescr.33 = STRING: GigabitEthernet0/0/24
IF-MIB::ifOperStatus.10 = INTEGER: up(1)
IF-MIB::ifOperStatus.11 = INTEGER: up(1)
IF-MIB::ifOperStatus.12 = INTEGER: up(1)
IF-MIB::ifOperStatus.13 = INTEGER: down(2)
IF-MIB::ifOperStatus.14 = INTEGER: down(2)
IF-MIB::ifOperStatus.15 = INTEGER: down(2)
IF-MIB::ifOperStatus.16 = INTEGER: down(2)
IF-MIB::ifOperStatus.17 = INTEGER: down(2)
IF-MIB::ifOperStatus.18 = INTEGER: down(2)
IF-MIB::ifOperStatus.19 = INTEGER: down(2)
IF-MIB::ifOperStatus.20 = INTEGER: down(2)
IF-MIB::ifOperStatus.21 = INTEGER: down(2)
IF-MIB::ifOperStatus.22 = INTEGER: down(2)
IF-MIB::ifOperStatus.23 = INTEGER: down(2)
IF-MIB::ifOperStatus.24 = INTEGER: down(2)
IF-MIB::ifOperStatus.25 = INTEGER: down(2)
IF-MIB::ifOperStatus.26 = INTEGER: down(2)
IF-MIB::ifOperStatus.27 = INTEGER: down(2)
IF-MIB::ifOperStatus.28 = INTEGER: down(2)
IF-MIB::ifOperStatus.29 = INTEGER: down(2)
IF-MIB::ifOperStatus.30 = INTEGER: down(2)
IF-MIB::ifOperStatus.31 = INTEGER: down(2)
IF-MIB::ifOperStatus.32 = INTEGER: down(2)
IF-MIB::ifOperStatus.33 = INTEGER: up(1)

port.bat代码:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "tokens=3 delims=:" %%a in ('type port.txt^|findstr /c:":"') do (
  4. set str=%%a
  5. set str=!str: =!
  6. echo !str!
  7. )
  8. pause
复制代码
怎么实现输出如下两列形式:
GigabitEthernet0/0/1 up(1)
GigabitEthernet0/0/2 up(1)
。。。。。。。。。。。
。。。。。。。。。。

这样?
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "tokens=* delims=" %%a in ('type port.txt^|findstr /c:":"') do (
  4.      for /f "tokens=3,5 delims=:.= " %%i in ("%%a") do (
  5.            if defined _%%i (echo !_%%i! %%j) else set "_%%i=%%j"
  6.      )
  7. )
  8. pause
复制代码

TOP

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "tokens=3-5 delims=:.= " %%i in (port.txt) do (
  4.     if "%%j"=="STRING" (
  5.         set "_%%i=%%k"
  6.     ) else if "%%j"=="INTEGER" (
  7.         if defined _%%i echo,!_%%i! %%k
  8.     )
  9. )
  10. pause
复制代码

TOP

感谢老铁,完美运行

TOP

本帖最后由 qiulai 于 2022-11-13 11:15 编辑

如果文本内容下面还有,怎么分成3列,4列……以此类推
比如这样
IF-MIB::ifDescr.10 = STRING: GigabitEthernet0/0/1
IF-MIB::ifDescr.11 = STRING: GigabitEthernet0/0/2
IF-MIB::ifDescr.12 = STRING: GigabitEthernet0/0/3
IF-MIB::ifDescr.13 = STRING: GigabitEthernet0/0/4
IF-MIB::ifOperStatus.10 = INTEGER: up(1)
IF-MIB::ifOperStatus.11 = INTEGER: up(1)
IF-MIB::ifOperStatus.12 = INTEGER: up(1)
IF-MIB::ifOperStatus.13 = INTEGER: down(2)
IF-MIB::ifPhysAddress.10 = STRING: 4c:1f:cc:98:7b:64
IF-MIB::ifPhysAddress.11 = STRING: 4c:1f:cc:98:7b:64
IF-MIB::ifPhysAddress.12 = STRING: 4c:1f:cc:98:7b:64
IF-MIB::ifPhysAddress.13 = STRING: 4c:1f:cc:98:7b:64
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
GigabitEthernet0/0/1 up 4c:1f:cc:98:7b:64
GigabitEthernet0/0/2 up 4c:1f:cc:98:7b:65
GigabitEthernet0/0/3 up 4c:1f:cc:98:7b:66
GigabitEthernet0/0/4 down 4c:1f:cc:98:7b:67

TOP

本帖最后由 qiulai 于 2022-11-13 10:00 编辑

老铁,自定义列数怎么改下你这代码,如下面这样,
GigabitEthernet0/0/1 up 4c:1f:cc:98:7b:64 ……
GigabitEthernet0/0/2 up 4c:1f:cc:98:7b:65 ……
GigabitEthernet0/0/3 up 4c:1f:cc:98:7b:66 ……
GigabitEthernet0/0/4 down 4c:1f:cc:98:7b:67 ……

TOP

没明白何谓“固定行分列”、“自定义列数”。

TOP

回复 5# qiulai


   

将:
  1. IF-MIB::ifDescr.10 = STRING: GigabitEthernet0/0/1
  2. IF-MIB::ifDescr.11 = STRING: GigabitEthernet0/0/2
  3. IF-MIB::ifDescr.12 = STRING: GigabitEthernet0/0/3
  4. IF-MIB::ifDescr.13 = STRING: GigabitEthernet0/0/4
  5. IF-MIB::ifOperStatus.10 = INTEGER: up(1)
  6. IF-MIB::ifOperStatus.11 = INTEGER: up(1)
  7. IF-MIB::ifOperStatus.12 = INTEGER: up(1)
  8. IF-MIB::ifOperStatus.13 = INTEGER: down(2)
  9. IF-MIB::ifPhysAddress.10 = STRING: 4c:1f:cc:98:7b:64
  10. IF-MIB::ifPhysAddress.11 = STRING: 4c:1f:cc:98:7b:64
  11. IF-MIB::ifPhysAddress.12 = STRING: 4c:1f:cc:98:7b:64
  12. IF-MIB::ifPhysAddress.13 = STRING: 4c:1f:cc:98:7b:64
复制代码
以ANSI编码格式保存为port.txt
下载gawk( http://bcn.bathome.net/tool/4.1.0/gawk.exe
执行:
  1. gawk -F"\\.|::| = |: " "!($2 in a){a[$2];b[++N]=$2}!($3 in a){a[$3];c[++n]=$3}{d[$2,$3]=$5}END{for(i=0;i++<n;){for(j=0;j++<N;){printf\"%s \",d[b[j],c[i]]}print\"\"}}" port.txt>out.txt
复制代码
结果(out.txt):
  1. GigabitEthernet0/0/1 up(1) 4c:1f:cc:98:7b:64
  2. GigabitEthernet0/0/2 up(1) 4c:1f:cc:98:7b:64
  3. GigabitEthernet0/0/3 up(1) 4c:1f:cc:98:7b:64
  4. GigabitEthernet0/0/4 down(2) 4c:1f:cc:98:7b:64
复制代码

TOP

  1. @echo off
  2. rem 保存为ANIS文件
  3. setlocal enabledelayedexpansion
  4. for /f "tokens=1* delims==" %%a in ('type port.txt^|findstr /c:":"') do (
  5.      for /f "tokens=*" %%c in ("%%b") do (
  6.            if not defined _%%~xa (
  7.                     set /a "_%%~xa=n+=1"
  8.                     set "s_!n!=%%c"
  9.            ) else  for %%k in (!_%%~xa!) do set "s_%%k=!s_%%k! %%b
  10.      )
  11. )
  12. for /l %%i in (1,1,!n!) do for %%j in (%%i) do echo !s_%%j!
  13. pause
复制代码

TOP

terse 发表于 2022-11-13 14:36



    测试结果如下
STRING: GigabitEthernet0/0/1  INTEGER: up(1)  STRING: 4c:1f:cc:98:7b:64
STRING: GigabitEthernet0/0/2  INTEGER: up(1)  STRING: 4c:1f:cc:98:7b:64
STRING: GigabitEthernet0/0/3  INTEGER: up(1)  STRING: 4c:1f:cc:98:7b:64
STRING: GigabitEthernet0/0/4  INTEGER: down(2)  STRING: 4c:1f:cc:98:7b:64
有没有大神能把STRING:和INTEGER: 去掉就完美了

TOP

回复  qiulai


   

将:以ANSI编码格式保存为port.txt
下载gawk(  )
执行:结果(out.txt):
hfxiang 发表于 2022-11-13 13:07



    运行后是下面结果
s s s
s s s
s s s
s s s

TOP

回复 11# qiulai


   
要确保脚本及输入文本均已保存为 ANSI 编码格式

TOP

  1. @echo off
  2. rem 保存为 ANSI 编码格式
  3. setlocal enabledelayedexpansion
  4. for /f "tokens=1* delims==" %%a in ('type port.txt^|findstr /c:":"') do (
  5.      for /f "tokens=2" %%c in ("%%b") do (
  6.            if not defined _%%~xa (
  7.                     set /a "_%%~xa=n+=1"
  8.                     set "s_!n!=%%c"
  9.            ) else  for %%k in (!_%%~xa!) do set "s_%%k=!s_%%k! %%c
  10.      )
  11. )
  12. for /l %%i in (1,1,!n!) do for %%j in (%%i) do echo !s_%%j!
  13. pause
复制代码
回复 10# qiulai


也GAWK来搞一下
  1. gawk -F"[= .]" "{arr[$2]=arr[$2] ?arr[$2]\" \"$NF : $NF }END{  for (id in arr) print arr[id]}" port.txt
复制代码
1

评分人数

TOP

回复  qiulai


   

将:以ANSI编码格式保存为port.txt
下载gawk(  )
执行:结果(out.txt):
hfxiang 发表于 2022-11-13 13:07



    我看你有三套房,卖了两套还真有一套!运行成功

TOP

回复 13# terse


   
厉害,大神写的awk代码太令人佩服了

TOP

返回列表