2013-10-19 70 views
1

在批處理文件中使用下面的命令,我得到輸出,但還有一些回聲作爲回車。從WMIC輸出中刪除回車

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

輸出:

ProcessId WorkingSetSize 
4016  6356992   
1548  6189056   

如何擺脫在輸出這個回車?

EDIT1:

參考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 
4768  6365184   
5608  5500928   
2888  6037504   
1052  5472256   
ECHO is off. 

EDIT2:

ProcessId WorkingSetSize 
    4768  6365184   
    5608  5500928   
    2888  6037504   
    1052  5472256 

我想在上面的格式輸出只和獲得使用輸出:

@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都可以命令沒有在我的系統工作.. :(..會感謝您的確認.. :)

+1

可能重複的[編輯WMIC輸出格式](HTTP://計算器.com/questions/19462042/editing-wmic-output-format) – nephi12

+0

@foxidrive請參閱** EDIT1 **問題.. :) – Sunny

+0

是的..我需要這些值在第二個腳本中,但是當一個額外的CR來在輸出中,第二個腳本不起作用。 – Sunny

回答

1

這消除了拖尾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 
+0

@ Foxi ..感謝..!這兩個代碼在我的系統上運行良好..請問您可以確認** EDIT2 **問題..? – Sunny

1

試試這個:

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

輸出是(Windows 8中)遠離'太可怕了」

 
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 

+1

這是一個可怕的輸出來解析Endoro - 空間嘉豪和CR仍然存在。 – foxidrive

+0

是Endoro ....空間回聲仍然在輸出。 – Sunny

+1

@ foxidrive在敘利亞的戰爭是_horrible_,我的建議的輸出是不是。你可以看看我的解釋。 – Endoro

0

我有運行代碼沒有多餘的CR或aditional的回聲。但是,既然你得到了他們,Endoro的回答就夠了。而不是試圖不期望的線路進行篩選,試圖讓只有你有興趣

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

只有一個數字(數據)或P(的ProcessID)開始的行線

+0

@ MC ..而使用'wmic進程,其中Caption ='notepad.exe'得到ProcessId,WorkingSetSize/Format:Texttable^| findstr/B/R「[0-9P]」> file.txt'得到以下錯誤:'無效的XSL格式或文件名' – Sunny

+0

我正在使用^ |在for命令的引用字符串中。如果從命令行使用,請刪除^ –

+1

如果您沒有得到結尾CR,您使用的是哪個操作系統?你真的使用十六進制查看器來檢查? – foxidrive