C:\_G\WWW\~ELISANET\INFO\tscmd114.html
<http://www.elisanet.fi/tsalmi/info/tscmd114.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Fri 30-Nov-2018 11:42:49

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



114} How do I pick a file's lines that are three characters long?

Q: I have a text file with a list of around 900 names in it. I'd like to be able to retrieve all the names that are three characters long.

The method is an adaptation of items #26 and #27 of this FAQ.
  @echo off & setlocal enableextensions enabledelayedexpansion
  for /f "delims=" %%a in ('type myfile.txt') do (
    set name_=%%a
    set charCount=0
    for /l %%c in (0,1,255) do (
      set si=!name_:~%%c,1!
      if defined si set /a charCount+=1)
    if !charCount! EQU 3 echo !name_!
    )
  endlocal & goto :EOF
This method cannot handle names with the "poison characters".

However, a much simpler and still an all-script solution is
  findstr "^...$" "My test file.txt"

With sed it would be
  <"My test file.txt" sed -n "s/^...$/&/p"

Note the redirection formulation instead of using
  sed -n "s/^...$/&/p" "My test file.txt"
which may fail depending on the SED port version LFN handling.

With gawk
  <"My test file.txt" gawk "{if(length()==3)printf\"%%s\n\",$0}"

What if you want all the names that are three or less characters long?
  findstr "^.$ ^..$ ^...$" "My test file.txt"

References/Comments:
  hh ntcmds.chm::/findstr.htm [You can't use this unless you have the ntcmds.chm file]

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd114.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd114
http://www.elisanet.fi/tsalmi/info/tscmd114.html
file:///c:/_g/www/~elisanet/info/tscmd114.html