how to extract only Capital Letters from file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

how to extract only Capital Letters from file

#1 Post by timbertuck » 17 Nov 2012 11:05

i have a test file that contains the following as an example
testfile
A
some text
and some more text
B; B-C;DE
some more text
lots of text
E F
other text
text text text


and need it to output only the CAPS words but not the associated text
A
B
B-C
DE
E F

so the file can contain a single CAP word, two or more CAP words separated by a semi-colon and a CAP word with an embedded space.

i have come up with the following but am stuck on the rest.

Code: Select all

@echo oFF
setlocal enableextensions enabledelayedexpansion
set "$s=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "$t=;"
echo.>tmp.tmp
for /f "tokens=1-5* delims=; " %%a in ('type testfile') do (
   REM IF %%a IsAllCaps ()   "how to make this line work"
   find "%%a" tmp.tmp>nul
   set erl=!errorlevel!
   if !erl!==1 (
      echo %%a>>tmp.tmp
      if "%%b"=="" (ECHO.>nul) ELSE echo %%b>>tmp.tmp
      if "%%c"=="" (ECHO.>nul) ELSE echo %%c>>tmp.tmp
      if "%%d"=="" (ECHO.>nul) ELSE echo %%d>>tmp.tmp
      if "%%e"=="" (ECHO.>nul) ELSE echo %%e>>tmp.tmp
      )
)
type tmp.tmp


is there code out there that only works on CAP'ped letters?

appreciate the help.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to extract only Capital Letters from file

#2 Post by aGerman » 17 Nov 2012 11:51

FINDSTR supports regular expressions.
The following should work

Code: Select all

@echo off &setlocal
for /f "delims=" %%i in ('findstr "\<[ABCDEFGHIJKLMNOPQRSTUVWXYZ-]*\>" "test.txt"') do (
  set "ln=%%i"
  setlocal EnableDelayedExpansion
  for %%j in (!ln!) do (
    endlocal
    set "word=%%j"
    setlocal EnableDelayedExpansion
    echo !word!|findstr /rxc:"[ABCDEFGHIJKLMNOPQRSTUVWXYZ-]*"
  )
  endlocal
)
pause

Regards
aGerman

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to extract only Capital Letters from file

#3 Post by Aacini » 17 Nov 2012 11:54

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set upcaseLetters=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
for /F "delims=" %%a in (testfile.txt) do (
   for %%b in (%%a) do (
      set "word=%%b"
      for %%c in (%upcaseLetters%) do set "word=!word:%%c=%%c!"
      if "!word!" equ "%%b" echo %%b
   )
)

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: how to extract only Capital Letters from file

#4 Post by timbertuck » 21 Nov 2012 13:38

Aacini wrote:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set upcaseLetters=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
for /F "delims=" %%a in (testfile.txt) do (
   for %%b in (%%a) do (
      set "word=%%b"
      for %%c in (%upcaseLetters%) do set "word=!word:%%c=%%c!"
      if "!word!" equ "%%b" echo %%b
   )
)


thanks aacini, i got this running with one error, it says "Missing operand." and this is due to the /set /a eval1 line. i do this line to test for numbers which i exclude from tmp.tmp. it does work in that it only adds' capital WORDS. but i would like to get rid of the error.

appreciate the help

Code: Select all

@echo off
ECHO.>TMP.TMP
setlocal EnableDelayedExpansion
set upcaseLetters=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
for /F "delims=.()" %%a in (C:\users\dan3\downloads\29765-8.txt) do (
   for %%b in (%%a) do (
      set "word=%%b"
      for %%c in (%upcaseLetters%) do set "word=!word:%%c=%%c!"
    if "!word!" equ "%%b" (
      SET /A EVAL1="%%b" * 10
      FIND "!WORD!" TMP.TMP>NUL
      if !eval1! equ 0 IF ERRORLEVEL 1 echo %%b>>TMP.TMP &ECHO ADDING...)
   )
)

TYPE TMP.TMP
Last edited by timbertuck on 21 Nov 2012 13:41, edited 1 time in total.

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: how to extract only Capital Letters from file

#5 Post by timbertuck » 21 Nov 2012 13:41

aGerman wrote:FINDSTR supports regular expressions.
The following should work

Code: Select all

@echo off &setlocal
for /f "delims=" %%i in ('findstr "\<[ABCDEFGHIJKLMNOPQRSTUVWXYZ-]*\>" "test.txt"') do (
  set "ln=%%i"
  setlocal EnableDelayedExpansion
  for %%j in (!ln!) do (
    endlocal
    set "word=%%j"
    setlocal EnableDelayedExpansion
    echo !word!|findstr /rxc:"[ABCDEFGHIJKLMNOPQRSTUVWXYZ-]*"
  )
  endlocal
)
pause

Regards
aGerman


thanks aGerman for lending a hand. i tried to adapt your program but couldn't get it to save to a tmp file. below is as far as i got. how can i save the output to a file using your solution.

Code: Select all

@echo off
setlocal
echo.>tmp.tmp
for /f "delims=" %%i in ('findstr "\<[ABCDEFGHIJKLMNOPQRSTUVWXYZ-]*\>" "TESTfile.txt"') do (
  set "ln=%%i"
  setlocal EnableDelayedExpansion
  for %%j in (!ln!) do (
    endlocal
    set "word=%%j"
    setlocal EnableDelayedExpansion
    (echo !word!|findstr /rxc:"[ABCDEFGHIJKLMNOPQRSTUVWXYZ-]*") || ECHO !WORD!>>TMP.TMP
   )
  endlocal
)
pause
type tmp.tmp

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: how to extract only Capital Letters from file

#6 Post by Squashman » 21 Nov 2012 13:55

I am confused why you are trying to use a conditional execution to output the WORD variable to a temp file. That code will only execute when the previous command did not execute successfully.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: how to extract only Capital Letters from file

#7 Post by Ed Dyreen » 21 Nov 2012 14:00

'
I tried to formulate an answer, but gave up as I found too little information provided in the original post.
People have no clue which OS you are on nor whether empty lines need to be preserved :?

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: how to extract only Capital Letters from file

#8 Post by timbertuck » 21 Nov 2012 14:42

Squashman wrote:I am confused why you are trying to use a conditional execution to output the WORD variable to a temp file. That code will only execute when the previous command did not execute successfully.


im not sure either now that i look at it. what i wanted was to echo the CAP letters (WORDS actually) into a tmp file. i tried to ">>tmp.tmp after the findstr but that didn't work. how would i make the output of the CAP words into a file?

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: how to extract only Capital Letters from file

#9 Post by timbertuck » 21 Nov 2012 14:47

Ed Dyreen wrote:'
I tried to formulate an answer, but gave up as I found too little information provided in the original post.
People have no clue which OS you are on nor whether empty lines need to be preserved :?


On windows 7 64bit. no to the empty lines being preserved. the original file is 28mb. if you want i can post a sample of the file, but essentially it is as i originally posted.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how to extract only Capital Letters from file

#10 Post by aGerman » 21 Nov 2012 17:28

timbertuck wrote:i tried to adapt your program but couldn't get it to save to a tmp file. below is as far as i got. how can i save the output to a file using your solution.


Enclose the loop into another pair of parentheses to catch the outgoing stream and redirect it into your temporary file.

Code: Select all

@echo off &setlocal
>"tmp.tmp" (
  for /f "delims=" %%i in ('findstr "\<[ABCDEFGHIJKLMNOPQRSTUVWXYZ-]*\>" "TESTfile.txt"') do (
    set "ln=%%i"
    setlocal EnableDelayedExpansion
    for %%j in (!ln!) do (
      endlocal
      set "word=%%j"
      setlocal EnableDelayedExpansion
      echo !word!|findstr /rxc:"[ABCDEFGHIJKLMNOPQRSTUVWXYZ-]*"
    )
    endlocal
  )
)
type "tmp.tmp"
pause

Regards
aGerman

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: how to extract only Capital Letters from file

#11 Post by timbertuck » 22 Nov 2012 09:58

aGerman wrote:Enclose the loop into another pair of parentheses to catch the outgoing stream and redirect it into your temporary file.

Code: Select all

@echo off &setlocal
>"tmp.tmp" (
  for /f "delims=" %%i in ('findstr "\<[ABCDEFGHIJKLMNOPQRSTUVWXYZ-]*\>" "TESTfile.txt"') do (
    set "ln=%%i"
    setlocal EnableDelayedExpansion
    for %%j in (!ln!) do (
      endlocal
      set "word=%%j"
      setlocal EnableDelayedExpansion
      echo !word!|findstr /rxc:"[ABCDEFGHIJKLMNOPQRSTUVWXYZ-]*"
    )
    endlocal
  )
)
type "tmp.tmp"
pause

Regards
aGerman


only one word for that, Suh-weeet! :mrgreen: now this way any output from a for loop can be captured, i like it.

and only one word for you aGerman, Brainiac! :P

thanks again

Post Reply