Home > Back-end >  What is the command in cmd to count words after a particular word?
What is the command in cmd to count words after a particular word?

Time:03-23

I am facing the following issue. I am on Windows, using cmd and typing this command on cmd findstr "applications" test.txt. The result is something like this:

applications: sample1, guessGame, test1233, testVibeeer, sofiaa

What would be the command to get the number of applications? I have tried with findstr "applications" test.txt | find /c /v "" but it doesn't work. I would like to get a result of 5 in this case but get different result.

CodePudding user response:

try this command.

awk -F "," '{print NF}' test.dat

enter image description here

CodePudding user response:

if you wanna do it in the most awk-ish of minimalist coding, it's

  gawk/nawk  -F, '$ _=NF  '

  mawk/mawk2 -F, 'NF=$_=NF' 

CodePudding user response:

use a for /f loop to get the relevant line, then count the tokens after the colon with a pure for loop:

set count=0
for /f "tokens=2 delims=:" %%a in ('find "applications" test.txt') do (
  for %%b in (%%a) do set /a count =1
)
echo %count%

(note, find /c finds lines that contain the search string, so your approach is doomed from the start)

For usage directly on the command line:

set count=0
@for /f "tokens=2 delims=:" %a in ('find "applications" test.txt') do @for %b in (%a) do @set /a count =1 >nul
echo %count%
  • Related