7

The following content is stored in a file:

chrome.exe                   512 Console                 0     73,780 K
chrome.exe                   800 Console                 0     11,052 K
chrome.exe                  1488 Console                 0     92,720 K
chrome.exe                  1600 Console                 0     32,344 K
chrome.exe                  2240 Console                 0     35,132 K
chrome.exe                  2360 Console                 0     21,276 K
chrome.exe                  3524 Console                 0     66,732 K
chrome.exe                  3924 Console                 0     23,524 K

Is there a way to extract the 5th column with the Windows command line?

Something like the UNIX cut command.

daaawx
  • 3,273
  • 2
  • 17
  • 16
Vineel Kumar Reddy
  • 4,588
  • 9
  • 33
  • 37

5 Answers5

8

Use double % in variable

for /f "tokens=5 delims= " %%i in (file.txt) DO echo %%i
wscourge
  • 10,657
  • 14
  • 59
  • 80
Sonny Saluja
  • 7,193
  • 2
  • 25
  • 39
  • Long time to pump up this thread, after ran like as your code, I got on my Windows 10: %%i was unexpected at this time. – tquang Feb 14 '23 at 05:32
6

If you're familiar with the GNU cut utility, you might be better off using the Win32 port:

http://gnuwin32.sourceforge.net/packages/coreutils.htm

bcosca
  • 17,371
  • 5
  • 40
  • 51
3
@ECHO OFF

for /F "tokens=2-4" %%a in (%1) DO ( echo %%a %%b %%c )

took me a long time to find out that %%a %%b %%c .... [%%z] refer to subsequent colums in a text file. So this example will extract the 2nd, 3rd and 4th column (word) from a textfile (%1).

Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
-1

You can also use mobaxterm (https://mobaxterm.mobatek.net/) a ssh client, and open a local terminal. It contain unix comands like cut, grap, wc ,etc

adilazh1
  • 29
  • 1
-3

If you had perl installed:

perl.exe -na  -e "print qq{$F[4]\n}" < myfile.txt
knb
  • 9,138
  • 4
  • 58
  • 85