Link to home
Start Free TrialLog in
Avatar of samiam41
samiam41Flag for United States of America

asked on

Remove column in output file from batch script

Hi Experts.  I'm using the following command to output a list of processes running on a computer.  I wanted to remove some of the columns that are being written to the log file but wasn't sure if it could be done.

pslist | findstr.exe /v /i /r /c:"^chrome\>" /c:"^sVchost\>" >> %log%

Open in new window


I appreciate your help with this!
Avatar of oBdA
oBdA

If the output doesn't have be as pretty as the one generated from pslist, you can use something like this.
In line 4, you define the header you want, separated by tabs.
In line 6, you define the columns by letter, with %%a=col1, and so on.
@echo off
setlocal
set Log=C:\Temp\log.txt
>"%Log%" echo Name	PID	Priv	CPU
for /f "skip=3 tokens=1-8" %%a in ('pslist.exe ^| findstr.exe /v /i /r /c:"^chrome\>" /c:"^sVchost\>"') do (
	>>"%Log%" echo %%a	%%b	%%f	%%g
)

Open in new window

Avatar of samiam41

ASKER

Thank you.  Testing it now.
There are 8 columns in the output log file.  So, using for /f to read each column as %%a, %%b, and so on.
Just remove the %%x which corresponding to the column you don't want from the echo line

for /f "tokens=1-8 skip=2" %%a in (c:\temp\log.txt) do (
echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h >> %log%
)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@oBdA, here is what I have for the script.  could you please review and see what I did wrong as all of the columns are showing up even though I modified the "set columns" option (I only pasted the top portion of the script as that's the only location containing what I added) .

@echo off
setlocal enabledelayedexpansion
set Log=c:\tools\logs\audit.log
set Columns=1 2 

cls

cd\
cd tools\pstools

echo. >> %log%
echo. >> %log%

echo %date% %time: =0% >> %log%

Open in new window

@tgtran, can you put together your portion of the script with what I posted, please?  I'm not sure if I am putting them together correctly.

pslist | findstr.exe /v /i /r /c:"^chrome\>" /c:"^sVchost\>" >> %log%

for /f "tokens=1-8 skip=2" %%a in (c:\temp\log.txt) do (
echo %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h >> %log%
)

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@tgtran,

Using your script above, this is what log2 yields:

Name,Time 
Idle,329:07:01.747 
System,329:07:01.747 
smss,329:07:01.622 
csrss,329:06:55.616 
wininit,329:06:52.090 
csrss,329:06:52.075 
services,329:06:51.966 
lsass,329:06:51.888 
lsm,329:06:51.888 
winlogon,329:06:51.201 
nvvsvc,329:06:47.488 
nvwmi64,329:06:47.239 
nvSCPAPISvr,329:06:47.223 
spoolsv,329:06:28.751 
armsvc,329:06:28.236 
AppleMobileDeviceService,329:06:27.986 
atashost,329:06:26.738 
mDNSResponder,329:06:26.613 
BtSwitcherService,329:06:26.598 
CsrBtOBEXService,329:06:26.239 
CsrBtService,329:06:26.145 
DisplayFusionService,329:06:25.677 

Open in new window


How do I get the columns back and make it easier to read?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your time and suggestions on this question!!

oBdA's script won out as it was easier to configure the look of the output file but tgtran solution worked so I gave partial credit/points.

Again, always glad to work with experts like you all.  Take care!
Anyone interested in a few more points, I would appreciate you stopping by here.

https://www.experts-exchange.com/questions/28931915/Add-content-to-output-file.html