Batch file 从WMIC输出中删除回车符

Batch file 从WMIC输出中删除回车符,batch-file,Batch File,在一个批处理文件中使用下面的命令,我得到了输出,但伴随着一些作为回车符的回声 wmic process where Caption='notepad.exe' get ProcessId,WorkingSetSize /Format:Texttable > output.txt 输出: ProcessId WorkingSetSize 4016 6356992 1548 6189056 ProcessId Workin

在一个批处理文件中使用下面的命令,我得到了输出,但伴随着一些作为回车符的回声

wmic process where Caption='notepad.exe' get ProcessId,WorkingSetSize /Format:Texttable > output.txt
输出:

ProcessId  WorkingSetSize  
4016       6356992         
1548       6189056         
ProcessId  WorkingSetSize  
4768       6365184         
5608       5500928         
2888       6037504         
1052       5472256         
ECHO is off.
ProcessId  WorkingSetSize  
    4768       6365184         
    5608       5500928         
    2888       6037504         
    1052       5472256  
C:\Users\Private\TEST>wmic process where Caption='notepad.exe' get ProcessId,WorkingSetSize /Format:Texttable|findstr /v "^$">file.txt C:\Users\Private\TEST>type file.txt ProcessId WorkingSetSize 8896 8142848 C:\Users\Private\TEST>hexdump -C file.txt 00000000 50 72 6f 63 65 73 73 49 64 20 20 57 6f 72 6b 69 |ProcessId Worki| 00000010 6e 67 53 65 74 53 69 7a 65 20 20 0d 0d 0a 38 38 |ngSetSize ...88| 00000020 39 36 20 20 20 20 20 20 20 38 31 34 32 38 34 38 |96 8142848| 00000030 20 20 20 20 20 20 20 20 20 0d 0d 0a | ...| 0000003c C:\Users\Private\TEST>for /f "tokens=*" %a in (file.txt) do @echo %a ProcessId WorkingSetSize 8896 8482816 C:\Users\Private\TEST>(for /f "tokens=*" %a in (file.txt) do @echo %a)|hexdump -C 00000000 50 72 6f 63 65 73 73 49 64 20 20 57 6f 72 6b 69 |ProcessId Worki| 00000010 6e 67 53 65 74 53 69 7a 65 20 20 0d 20 0a 38 38 |ngSetSize . .88| 00000020 39 36 20 20 20 20 20 20 20 38 34 38 32 38 31 36 |96 8482816| 00000030 20 20 20 20 20 20 20 20 20 0d 20 0a | . .| 0000003c 如何在输出中消除此回车

EDIT1:

ProcessId  WorkingSetSize  
4016       6356992         
1548       6189056         
ProcessId  WorkingSetSize  
4768       6365184         
5608       5500928         
2888       6037504         
1052       5472256         
ECHO is off.
ProcessId  WorkingSetSize  
    4768       6365184         
    5608       5500928         
    2888       6037504         
    1052       5472256  
C:\Users\Private\TEST>wmic process where Caption='notepad.exe' get ProcessId,WorkingSetSize /Format:Texttable|findstr /v "^$">file.txt C:\Users\Private\TEST>type file.txt ProcessId WorkingSetSize 8896 8142848 C:\Users\Private\TEST>hexdump -C file.txt 00000000 50 72 6f 63 65 73 73 49 64 20 20 57 6f 72 6b 69 |ProcessId Worki| 00000010 6e 67 53 65 74 53 69 7a 65 20 20 0d 0d 0a 38 38 |ngSetSize ...88| 00000020 39 36 20 20 20 20 20 20 20 38 31 34 32 38 34 38 |96 8142848| 00000030 20 20 20 20 20 20 20 20 20 0d 0d 0a | ...| 0000003c C:\Users\Private\TEST>for /f "tokens=*" %a in (file.txt) do @echo %a ProcessId WorkingSetSize 8896 8482816 C:\Users\Private\TEST>(for /f "tokens=*" %a in (file.txt) do @echo %a)|hexdump -C 00000000 50 72 6f 63 65 73 73 49 64 20 20 57 6f 72 6b 69 |ProcessId Worki| 00000010 6e 67 53 65 74 53 69 7a 65 20 20 0d 20 0a 38 38 |ngSetSize . .88| 00000020 39 36 20 20 20 20 20 20 20 38 34 38 32 38 31 36 |96 8482816| 00000030 20 20 20 20 20 20 20 20 20 0d 20 0a | . .| 0000003c 我在下面的链接问题中引用了Foxdrive的工作答案,效果不错,但在输出中有一行额外的内容:ECHO关闭。不知道为什么

@echo off
setlocal enabledelayedexpansion
(For /F "tokens=1,* delims==" %%A in (' "wmic process where Caption='notepad.exe' get ProcessId,WorkingSetSize /Format:Texttable" ') do (
set "line=%%A %%B"
set "line=!line:~0,-1!"
echo !line!
))>output.txt
output.txt:

ProcessId  WorkingSetSize  
4016       6356992         
1548       6189056         
ProcessId  WorkingSetSize  
4768       6365184         
5608       5500928         
2888       6037504         
1052       5472256         
ECHO is off.
ProcessId  WorkingSetSize  
    4768       6365184         
    5608       5500928         
    2888       6037504         
    1052       5472256  
C:\Users\Private\TEST>wmic process where Caption='notepad.exe' get ProcessId,WorkingSetSize /Format:Texttable|findstr /v "^$">file.txt C:\Users\Private\TEST>type file.txt ProcessId WorkingSetSize 8896 8142848 C:\Users\Private\TEST>hexdump -C file.txt 00000000 50 72 6f 63 65 73 73 49 64 20 20 57 6f 72 6b 69 |ProcessId Worki| 00000010 6e 67 53 65 74 53 69 7a 65 20 20 0d 0d 0a 38 38 |ngSetSize ...88| 00000020 39 36 20 20 20 20 20 20 20 38 31 34 32 38 34 38 |96 8142848| 00000030 20 20 20 20 20 20 20 20 20 0d 0d 0a | ...| 0000003c C:\Users\Private\TEST>for /f "tokens=*" %a in (file.txt) do @echo %a ProcessId WorkingSetSize 8896 8482816 C:\Users\Private\TEST>(for /f "tokens=*" %a in (file.txt) do @echo %a)|hexdump -C 00000000 50 72 6f 63 65 73 73 49 64 20 20 57 6f 72 6b 69 |ProcessId Worki| 00000010 6e 67 53 65 74 53 69 7a 65 20 20 0d 20 0a 38 38 |ngSetSize . .88| 00000020 39 36 20 20 20 20 20 20 20 38 34 38 32 38 31 36 |96 8482816| 00000030 20 20 20 20 20 20 20 20 20 0d 20 0a | . .| 0000003c EDIT2:

ProcessId  WorkingSetSize  
4016       6356992         
1548       6189056         
ProcessId  WorkingSetSize  
4768       6365184         
5608       5500928         
2888       6037504         
1052       5472256         
ECHO is off.
ProcessId  WorkingSetSize  
    4768       6365184         
    5608       5500928         
    2888       6037504         
    1052       5472256  
C:\Users\Private\TEST>wmic process where Caption='notepad.exe' get ProcessId,WorkingSetSize /Format:Texttable|findstr /v "^$">file.txt C:\Users\Private\TEST>type file.txt ProcessId WorkingSetSize 8896 8142848 C:\Users\Private\TEST>hexdump -C file.txt 00000000 50 72 6f 63 65 73 73 49 64 20 20 57 6f 72 6b 69 |ProcessId Worki| 00000010 6e 67 53 65 74 53 69 7a 65 20 20 0d 0d 0a 38 38 |ngSetSize ...88| 00000020 39 36 20 20 20 20 20 20 20 38 31 34 32 38 34 38 |96 8142848| 00000030 20 20 20 20 20 20 20 20 20 0d 0d 0a | ...| 0000003c C:\Users\Private\TEST>for /f "tokens=*" %a in (file.txt) do @echo %a ProcessId WorkingSetSize 8896 8482816 C:\Users\Private\TEST>(for /f "tokens=*" %a in (file.txt) do @echo %a)|hexdump -C 00000000 50 72 6f 63 65 73 73 49 64 20 20 57 6f 72 6b 69 |ProcessId Worki| 00000010 6e 67 53 65 74 53 69 7a 65 20 20 0d 20 0a 38 38 |ngSetSize . .88| 00000020 39 36 20 20 20 20 20 20 20 38 34 38 32 38 31 36 |96 8482816| 00000030 20 20 20 20 20 20 20 20 20 0d 20 0a | . .| 0000003c 我只希望以上述格式输出,并使用以下方式获得该输出:

@echo off
setlocal enabledelayedexpansion
(For /F "delims=" %%A in ('"wmic process where Caption='notepad.exe' get  ProcessId,WorkingSetSize /format:Texttable |findstr "[0-9P]" "') do (
set "line=%%A"
echo !line:~0,-1!
)) > output.txt
现在输出中是否有任何后续CR?hexdump命令在我的系统中不起作用:(…请确认:)

尝试以下操作:

wmic process where Caption='notepad.exe' get ProcessId,WorkingSetSize /Format:Texttable|findstr /v "^$">file.txt

输出(Windows 8)远非“可怕”:

ProcessId  WorkingSetSize  
4016       6356992         
1548       6189056         
ProcessId  WorkingSetSize  
4768       6365184         
5608       5500928         
2888       6037504         
1052       5472256         
ECHO is off.
ProcessId  WorkingSetSize  
    4768       6365184         
    5608       5500928         
    2888       6037504         
    1052       5472256  
C:\Users\Private\TEST>wmic process where Caption='notepad.exe' get ProcessId,WorkingSetSize /Format:Texttable|findstr /v "^$">file.txt C:\Users\Private\TEST>type file.txt ProcessId WorkingSetSize 8896 8142848 C:\Users\Private\TEST>hexdump -C file.txt 00000000 50 72 6f 63 65 73 73 49 64 20 20 57 6f 72 6b 69 |ProcessId Worki| 00000010 6e 67 53 65 74 53 69 7a 65 20 20 0d 0d 0a 38 38 |ngSetSize ...88| 00000020 39 36 20 20 20 20 20 20 20 38 31 34 32 38 34 38 |96 8142848| 00000030 20 20 20 20 20 20 20 20 20 0d 0d 0a | ...| 0000003c C:\Users\Private\TEST>for /f "tokens=*" %a in (file.txt) do @echo %a ProcessId WorkingSetSize 8896 8482816 C:\Users\Private\TEST>(for /f "tokens=*" %a in (file.txt) do @echo %a)|hexdump -C 00000000 50 72 6f 63 65 73 73 49 64 20 20 57 6f 72 6b 69 |ProcessId Worki| 00000010 6e 67 53 65 74 53 69 7a 65 20 20 0d 20 0a 38 38 |ngSetSize . .88| 00000020 39 36 20 20 20 20 20 20 20 38 34 38 32 38 31 36 |96 8482816| 00000030 20 20 20 20 20 20 20 20 20 0d 20 0a | . .| 0000003c C:\Users\Private\TEST>wmic进程,其中Caption='notepad.exe'获取进程ID,WorkingSetSize/Format:Texttable | findstr/v“^$”>file.txt C:\Users\Private\TEST>type file.txt ProcessId工作集大小 8896 8142848 C:\Users\Private\TEST>hextump-C file.txt 00000000 50 72 6f 63 65 73 49 64 20 20 57 6f 72 6b 69 |进程ID工作| 00000010 6e 67 53 65 74 53 69 7a 65 20 20 0d 0d 0a 38 | ngSetSize…88| 00000020 39 36 20 20 20 20 20 20 20 38 31 34 32 38 34 38 |96 8142848| 0000003020200200D 0d 0a || 0000003c C:\Users\Private\TEST>for/f“tokens=*%a in(file.txt)do@echo%a ProcessId工作集大小 8896 8482816 C:\Users\Private\TEST>(对于(file.txt)do@echo%a中的/f“tokens=*%a)| hextump-C 00000000 50 72 6f 63 65 73 49 64 20 20 57 6f 72 6b 69 |进程ID工作| 00000010 6e 67 53 65 74 53 69 7a 65 20 20 0d 20 0a 38 38 | ngSetSize。88| 00000020 39 36 20 20 20 20 20 20 20 38 34 38 32 38 31 36 |96 8482816| 000000 30 20 20 20 20 0d 20 0a || 0000003c
我没有额外的CR或传统回声运行你的代码。但既然你们得到了答案,恩多的答案就足够接近了。不要试图过滤不需要的行,而是尝试只获取您感兴趣的行

wmic ..... ^| findstr /B /R "[0-9P]"

仅以数字(data)或p(ProcessId)开头的行

这将删除尾随CR并提供由空格分隔的值:

@echo off
setlocal enabledelayedexpansion
(For /F "tokens=2,3 delims=," %%a in ('"wmic process where Caption='notepad.exe' get ProcessId,WorkingSetSize /format:csv |findstr "[0-9]" "') do (
set "num=%%b"
echo %%a !num:~0,-1!
))>output.txt
该文件的格式如下:

624 4923392
9220 4886528

这将删除尾随CR并以CSV格式提供值

@echo off
setlocal enabledelayedexpansion
(For /F "delims=" %%A in ('"wmic process where Caption='notepad.exe' get ProcessId,WorkingSetSize /format:csv |findstr "[0-9]" "') do (
set "line=%%A"
echo !line:~0,-1!
))>output.txt
这是文件的格式:

PCNAME,956,4960256
PCNAME,4004,4870144

@foxidrive可能重复,请参见有问题的EDIT1.:)是的..我需要在第二个脚本中使用这些值,但是当输出中出现额外的CR时,第二个脚本不起作用。你的编辑2有尾随空格,但CR问题消失了。是否有删除尾随空格的建议?大量解析内多空格是一个可怕的输出,CR仍然存在。@foxidrive叙利亚战争很可怕,我的建议的输出不是。你可以看看我的解释@MC..当使用
wmic进程,其中Caption='notepad.exe'获取ProcessId时,WorkingSetSize/Format:Texttable^ | findstr/B/R“[0-9P]”file.txt
出现以下错误:
无效的XSL格式或文件名
我在for命令的引号内使用了^ |。如果从命令行中使用,请删除^如果您没有获得跟踪CR,您将使用哪些操作系统?你真的用十六进制查看器检查了吗?现在我明白你说的了。是的,CR在输出中。@Foxi..谢谢。。!这两个代码在我的系统上都运行良好。请您确认有问题的EDIT2。。?