Board logo

标题: [文本处理] 批处理怎么取到正确的列(某列可能为空)? [打印本页]

作者: ditto    时间: 2013-3-22 12:35     标题: 批处理怎么取到正确的列(某列可能为空)?

tasklist结果有5列,其中最后一列为内存
但可能第3列“会话”可能空,则会把第5列当第4列(本来想取5,864 ,结果取成了K)

映像名称                       PID     会话名              会话#       内存使用
========================= ======== ================ =========== ============
explorer.exe                  6748                                     4                5,864 K
explorer.exe                  4920 RDP-Tcp#82                 1              16,600 K

想用如下bat取第
  1. for /F "skip=3 tokens=5" %i in ('tasklist /fi "IMAGENAME eq explorer.exe"  ') do echo %i
复制代码
怎么才能取到5,864和16,600,而不是取K和16,600
作者: apang    时间: 2013-3-22 13:05

本帖最后由 apang 于 2013-3-22 13:06 编辑
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%a in ('tasklist /fi "imagename eq explorer.exe" /nh /fo csv') do (
  3.    for %%b in (%%a) do set "var=%%~b"
  4.    echo,!var: k=!
  5. )
  6. pause
复制代码

作者: terse    时间: 2013-3-22 13:17

  1. @echo off&setlocal enabledelayedexpansion
  2. for /F "tokens=*" %%i in ('tasklist /nh /fi "IMAGENAME eq explorer.exe" /FO CSV') do (
  3.     FOR %%a in (%%i) do set str=%%a
  4.     for /F %%a in (!str!) do set "str=%%a"
  5.     echo !str!
  6. )
  7. pause
复制代码

作者: ditto    时间: 2013-3-22 13:33

本帖最后由 ditto 于 2013-3-22 14:07 编辑

多谢2位  慢慢学习下
!str! 是啥意思
作者: CrLf    时间: 2013-3-22 19:37

  1. for /f tokens^=8delims^=^" %%a in ('tasklist /fi "imagename eq explorer.exe" /nh /fo csv') do echo %%a
复制代码

作者: BAT-VBS    时间: 2013-3-22 21:02

  1. tasklist /fi "IMAGENAME eq explorer.exe" 2>&1 | gawk "/explorer.exe/{print $(NF-1)}"
复制代码

作者: BAT-VBS    时间: 2013-3-22 21:09

  1. tasklist /fi "IMAGENAME eq explorer.exe" 2>&1 | sed "/explorer.exe/!d;s/.*[ \t]\([0-9,]\+\)[ \t]K/\1/"
复制代码

作者: CrLf    时间: 2013-3-23 00:59

回复 7# BAT-VBS
  1. tasklist /fi "IMAGENAME eq explorer.exe"|sed "s/.*  //"
复制代码

作者: BAT-VBS    时间: 2013-3-23 01:35

回复 8# CrLf


    不符合楼主要求吧




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2