Cutting strings where is the character "!"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Cutting strings where is the character "!"

#1 Post by RMSoares » 17 Oct 2012 06:38

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?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Cutting strings where is the character "!"

#2 Post by foxidrive » 17 Oct 2012 07:10

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Cutting strings where is the character "!"

#3 Post by abc0502 » 17 Oct 2012 07:30

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

RMSoares
Posts: 32
Joined: 06 Aug 2012 12:01

Re: Cutting strings where is the character "!"

#4 Post by RMSoares » 17 Oct 2012 08:01

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!

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

Re: Cutting strings where is the character "!"

#5 Post by Squashman » 17 Oct 2012 08:06

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.

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Cutting strings where is the character "!"

#6 Post by jeb » 17 Oct 2012 09:25

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Cutting strings where is the character "!"

#7 Post by foxidrive » 17 Oct 2012 09:51

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.

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Cutting strings where is the character "!"

#8 Post by jeb » 17 Oct 2012 13:22

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

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Cutting strings where is the character "!"

#9 Post by Liviu » 17 Oct 2012 20:15

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Cutting strings where is the character "!"

#10 Post by foxidrive » 17 Oct 2012 23:39

Thanks guys.

Post Reply