Page 1 of 1

Cutting strings where is the character "!"

Posted: 17 Oct 2012 06:38
by RMSoares
Hi,
I'm using a batch to cut the first 11 characters of each record in a text file.

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in (b.out) do (
set "Line=%%a"
>>d.out echo(!Line:~11!
)



My Input is a file b.out with fowlling record

312CCAAPT01 7106 GO!SERVICE - ONDERNEMINGEN KOMT - STAR XX!TECKDAILY - MASTER COMPANY, I123456789 YY!SERVICE - EXCHANGE 001ACTIVE FFBSTYUI 000000000000000000000000



It is intended that is removed from each record the first 11 caracters, passing the record being

CCAAPT01 7106 GO!SERVICE - ONDERNEMINGEN KOMT - STAR XX!TECKDAILY - MASTER COMPANY, I123456789 YY!SERVICE - EXCHANGE 001ACTIVE FFBSTYUI 000000000000000000000000


But what is happening because of the use of the character "!" Is not only removed the first 11 charcaters as is erroneously removed the character "!" and all the characters that are between two "!" are removed

CCAAPT01 7106 GOTECKDAILY - MASTER COMPANY, I123456789 YYSERVICE - EXCHANGE 001ACTIVE FFBSTYUI 000000000000000000000000


I do not know if with other characters ("#$%&/()=?) the batch will behave equally wrong as what's happening.

How can I do that batch will correctly intreprete special character ("!") as normal characters?

Re: Cutting strings where is the character "!"

Posted: 17 Oct 2012 07:10
by foxidrive
The problem is delayed expansion.

You can write a subroutine that the for-in-do can call and get around that. The characters ^ and % will still be a problem.

Re: Cutting strings where is the character "!"

Posted: 17 Oct 2012 07:30
by abc0502
why not using the set /p command it can handle the special characters and empty lines, have alook at this and this

Edit:
I don't know if that what you mean but you want this line:
312CCAAPT01 7106 GO!SERVICE - ONDERNEMINGEN KOMT - STAR XX!TECKDAILY - MASTER COMPANY, I123456789 YY!SERVICE - EXCHANGE 001ACTIVE FFBSTYUI 000000000000000000000000

without the blue letters and numbers

this should do it:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
<"b.out" (
   for /F "delims=" %%a in (b.out) do (
      set "ln="
      set /p "ln="
      echo !ln:~11!
   )
)
pause

Re: Cutting strings where is the character "!"

Posted: 17 Oct 2012 08:01
by RMSoares
abc0502 wrote:
Edit:
I don't know if that what you mean but you want this line:
312CCAAPT01 7106 GO!SERVICE - ONDERNEMINGEN KOMT - STAR XX!TECKDAILY - MASTER COMPANY, I123456789 YY!SERVICE - EXCHANGE 001ACTIVE FFBSTYUI 000000000000000000000000

without the blue letters and numbers


Now i see that are missing same sapces before "312CCAAPT01"

Thanks, the code it work's and solved my problem! That's it!

Re: Cutting strings where is the character "!"

Posted: 17 Oct 2012 08:06
by Squashman
312CCAAPT01 is the first 11 characters but you are saying you need the CCAAPT01 in your output.

If it is always the first 11 characters and those 11 characters do not have a space in them and the 12th character is always a space then delimit the line by a space and use the TOKENS=1* option as well.

Re: Cutting strings where is the character "!"

Posted: 17 Oct 2012 09:25
by jeb
foxidrive wrote:The problem is delayed expansion.

Yes this is the problem here :)

foxidrive wrote:You can write a subroutine that the for-in-do can call and get around that. The characters ^ and % will still be a problem.

No, it's quite easy.

You only need to disable the delayed Expansion sometimes (toggling it)

Code: Select all

@echo off
setlocal DisableDelayedExpansion

(
  for /F "delims=" %%a in (b.out) do (
    set "Line=%%a"
    setlocal EnableDelayedExpansion
    echo(!Line:~11!
    endlocal
  )
) > d.out


jeb

Re: Cutting strings where is the character "!"

Posted: 17 Oct 2012 09:51
by foxidrive
jeb wrote:You only need to disable the delayed Expansion sometimes (toggling it)

jeb


That's a good plan jeb. Are there any poison characters when using it this way?

I don't mean unicode or control characters, just plain text.

Re: Cutting strings where is the character "!"

Posted: 17 Oct 2012 13:22
by jeb
foxidrive wrote:That's a good plan jeb. Are there any poison characters when using it this way?

No, there aren't any special characters left, only the NULL (ASCII-0) character can't be read this way.
And you can't decide if a line ends with CRLF or only LF, but even CR's in the line can be read without problems.

The toggling technic, it is the best way to read text with problematic characters,
also as it can read to the limit of 8191 characters per lines .
But it's a bit tricky to get values over the endlocal barrier.
And theoretical it seems that it can read unlimited sized lines :o , but I don't know a way to access the content. :cry:


The set/p technic fails with some control characters and with lines longer than ~1021 characters,
but it has the advantage that you don't need to toggle, so it's much easier to use the values later.

jeb

Re: Cutting strings where is the character "!"

Posted: 17 Oct 2012 20:15
by Liviu
jeb wrote:No, there aren't any special characters left, only the NULL (ASCII-0) character can't be read this way.
jeb wrote:The set/p technic fails with some control characters and with lines longer than ~1021 characters, but it has the advantage that you don't need to toggle, so it's much easier to use the values later.

Just to fill in a couple of points too obvious for jeb to mention ;-) in the general case the for/f loop also needs to be secured against the accidental 'eol' (http://www.dostips.com/forum/viewtopic.php?f=3&t=2385), and it will always skip over empty lines. The set/p approach, on the other hand, needs the line count to be known in advance since it can't detect the end of file, and for lines longer than 1021 to read them piecewise and recombine the chunks (http://www.dostips.com/forum/viewtopic.php?p=13333#p13333).

Liviu

Re: Cutting strings where is the character "!"

Posted: 17 Oct 2012 23:39
by foxidrive
Thanks guys.