Page 1 of 1
how to extract only Capital Letters from file
Posted: 17 Nov 2012 11:05
by timbertuck
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.
Re: how to extract only Capital Letters from file
Posted: 17 Nov 2012 11:51
by aGerman
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
Re: how to extract only Capital Letters from file
Posted: 17 Nov 2012 11:54
by Aacini
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
)
)
Re: how to extract only Capital Letters from file
Posted: 21 Nov 2012 13:38
by timbertuck
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
Re: how to extract only Capital Letters from file
Posted: 21 Nov 2012 13:41
by timbertuck
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
Re: how to extract only Capital Letters from file
Posted: 21 Nov 2012 13:55
by Squashman
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.
Re: how to extract only Capital Letters from file
Posted: 21 Nov 2012 14:00
by Ed Dyreen
'
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
Re: how to extract only Capital Letters from file
Posted: 21 Nov 2012 14:42
by timbertuck
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?
Re: how to extract only Capital Letters from file
Posted: 21 Nov 2012 14:47
by timbertuck
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.
Re: how to extract only Capital Letters from file
Posted: 21 Nov 2012 17:28
by aGerman
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
Re: how to extract only Capital Letters from file
Posted: 22 Nov 2012 09:58
by timbertuck
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!
now this way any output from a for loop can be captured, i like it.
and only one word for you aGerman, Brainiac!
thanks again