标题: [文本处理] [已解决]批处理怎样把文本内容分列排序? [打印本页]
作者: qiulai 时间: 2022-11-12 23:37 标题: [已解决]批处理怎样把文本内容分列排序?
本帖最后由 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代码:- @echo off
- setlocal enabledelayedexpansion
- for /f "tokens=3 delims=:" %%a in ('type port.txt^|findstr /c:":"') do (
- set str=%%a
- set str=!str: =!
- echo !str!
- )
- pause
复制代码
怎么实现输出如下两列形式:
GigabitEthernet0/0/1 up(1)
GigabitEthernet0/0/2 up(1)
。。。。。。。。。。。
。。。。。。。。。。
作者: terse 时间: 2022-11-13 00:36
这样?- @echo off
- setlocal enabledelayedexpansion
- for /f "tokens=* delims=" %%a in ('type port.txt^|findstr /c:":"') do (
- for /f "tokens=3,5 delims=:.= " %%i in ("%%a") do (
- if defined _%%i (echo !_%%i! %%j) else set "_%%i=%%j"
- )
- )
- pause
复制代码
作者: qixiaobin0715 时间: 2022-11-13 08:22
- @echo off
- setlocal enabledelayedexpansion
- for /f "tokens=3-5 delims=:.= " %%i in (port.txt) do (
- if "%%j"=="STRING" (
- set "_%%i=%%k"
- ) else if "%%j"=="INTEGER" (
- if defined _%%i echo,!_%%i! %%k
- )
- )
- pause
复制代码
作者: qiulai 时间: 2022-11-13 09:01
感谢老铁,完美运行
作者: qiulai 时间: 2022-11-13 09:09
本帖最后由 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
作者: qiulai 时间: 2022-11-13 09:40
本帖最后由 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 ……
作者: qixiaobin0715 时间: 2022-11-13 09:53
没明白何谓“固定行分列”、“自定义列数”。
作者: hfxiang 时间: 2022-11-13 13:07
回复 5# qiulai
将:- 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
复制代码
以ANSI编码格式保存为port.txt
下载gawk( http://bcn.bathome.net/tool/4.1.0/gawk.exe )
执行:- 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):- GigabitEthernet0/0/1 up(1) 4c:1f:cc:98:7b:64
- GigabitEthernet0/0/2 up(1) 4c:1f:cc:98:7b:64
- GigabitEthernet0/0/3 up(1) 4c:1f:cc:98:7b:64
- GigabitEthernet0/0/4 down(2) 4c:1f:cc:98:7b:64
复制代码
作者: terse 时间: 2022-11-13 14:36
- @echo off
- rem 保存为ANIS文件
- setlocal enabledelayedexpansion
- for /f "tokens=1* delims==" %%a in ('type port.txt^|findstr /c:":"') do (
- for /f "tokens=*" %%c in ("%%b") do (
- if not defined _%%~xa (
- set /a "_%%~xa=n+=1"
- set "s_!n!=%%c"
- ) else for %%k in (!_%%~xa!) do set "s_%%k=!s_%%k! %%b
- )
- )
- for /l %%i in (1,1,!n!) do for %%j in (%%i) do echo !s_%%j!
- pause
复制代码
作者: qiulai 时间: 2022-11-13 16:06
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: 去掉就完美了
作者: qiulai 时间: 2022-11-13 16:07
回复 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
作者: hfxiang 时间: 2022-11-13 17:22
回复 11# qiulai
要确保脚本及输入文本均已保存为 ANSI 编码格式
作者: terse 时间: 2022-11-13 21:12
- @echo off
- rem 保存为 ANSI 编码格式
- setlocal enabledelayedexpansion
- for /f "tokens=1* delims==" %%a in ('type port.txt^|findstr /c:":"') do (
- for /f "tokens=2" %%c in ("%%b") do (
- if not defined _%%~xa (
- set /a "_%%~xa=n+=1"
- set "s_!n!=%%c"
- ) else for %%k in (!_%%~xa!) do set "s_%%k=!s_%%k! %%c
- )
- )
- for /l %%i in (1,1,!n!) do for %%j in (%%i) do echo !s_%%j!
- pause
复制代码
回复 10# qiulai
也GAWK来搞一下- gawk -F"[= .]" "{arr[$2]=arr[$2] ?arr[$2]\" \"$NF : $NF }END{ for (id in arr) print arr[id]}" port.txt
复制代码
作者: qiulai 时间: 2022-11-13 22:23
回复 qiulai
将:以ANSI编码格式保存为port.txt
下载gawk( )
执行:结果(out.txt):
hfxiang 发表于 2022-11-13 13:07
我看你有三套房,卖了两套还真有一套!运行成功
作者: hfxiang 时间: 2022-11-14 07:53
回复 13# terse
厉害,大神写的awk代码太令人佩服了
作者: qiulai 时间: 2022-11-14 20:28
回复 qiulai
也GAWK来搞一下
terse 发表于 2022-11-13 21:12
代码简短精干高效,牛顿的弟弟牛B!!!
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |