Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
JWinslow23
- Posts: 58
- Joined: 17 Jun 2014 10:38
#1
Post
by JWinslow23 » 06 Jul 2014 11:43
Is there a way to read a file character by character and put each character in an array if it is any of a few certain characters? For example, if the file is:
and the only allowed characters are in the string "+-<>.,[]", the expected contents of an array "chars" would be:
Code: Select all
chars[0]=","
chars[1]="["
chars[2]="."
chars[3]="-"
chars[4]="<"
chars[5]="]"
chars[6]="."
Is there any way that I can do that?
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#2
Post
by foxidrive » 06 Jul 2014 12:26
The actual scope of the file would be useful information, like so:
One line, 3000 characters?
Twenty lines, 40 characters each?
Are any of the characters Unicode or Non-Latin characters?
-
JWinslow23
- Posts: 58
- Joined: 17 Jun 2014 10:38
#3
Post
by JWinslow23 » 06 Jul 2014 12:56
foxidrive wrote:The actual scope of the file would be useful information, like so:
One line, 3000 characters?
Twenty lines, 40 characters each?
Are any of the characters Unicode or Non-Latin characters?
Well, it can vary. There could be one line with 3000 characters, there could be multiple lines with any amount of characters, maybe the same characters per line, maybe not, it depends.
And none of the characters are Unicode. They are ASCII values below 128.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#4
Post
by foxidrive » 06 Jul 2014 22:25
This uses a helper batch file called `repl.bat` (by dbenham) - download from:
https://www.dropbox.com/s/qidqwztmetbvklt/repl.batPlace `repl.bat` in the same folder as the batch file or in a folder that is on the path.
That's half the job.
Code: Select all
type file.txt |repl "[^+-<>.,\[\]]" "" >newfile.txt
This will separate each valid character onto it's own line.
Code: Select all
type file.txt |repl "[^+-<>.,\[\]]" "" |repl "(.)" "$1\r\n" xm
From there it's a simple loop to store them all in an array.
-
Aacini
- Expert
- Posts: 1914
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#5
Post
by Aacini » 07 Jul 2014 06:19
The solution below is short and simple, but may be somewhat slow. It also may have problems if the file have exclamation marks; both points may be fixed, if needed. This method can read lines up to 8192 characters long.
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "allowedChars=+-<>.,[]"
set i=-1
for /F "delims=" %%a in (test.txt) do (
for /F "delims=" %%b in ('cmd /U /C echo "%%a"^| find /V ""') do (
if "!allowedChars:%%b=!" neq "%allowedChars%" (
set /A i+=1
set chars[!i!]="%%b"
)
)
)
SET CHARS[
Output example:
Code: Select all
C:\> type test.txt
This, i[s a.tes t-c< as] e.
C:\> test.bat
chars[0]=","
chars[1]="["
chars[2]="."
chars[3]="-"
chars[4]="<"
chars[5]="]"
chars[6]="."
Antonio
-
JWinslow23
- Posts: 58
- Joined: 17 Jun 2014 10:38
#6
Post
by JWinslow23 » 07 Jul 2014 10:13
Well, that's nice, I gotta say. And thank you; I will not need to search for exclamation marks.