Page 1 of 1

trim off some characters of lines in txt file

Posted: 01 Aug 2017 09:49
by goodywp
Hi all forks,

So glad to be in this group and got some replies from you regarding this topic
"have a list of folder containing one file for each folder, need to find this file path in another folder/subfolder"

Now I had a solution for it but I need to trim off all the ==> for each line in one txt file as below:

==> 500007011000.S3S
==> 500011011000.S3S
==> 500016011000.S3S
==> 500072010400.S3S
==> 500136010100.S3S
==> 500022010300.S3S
==> 500007011000.S3S
==> 500008011000.S3S
==> 500019011200.S3S
==> 500008011000.S3S
==> 500009011000.S3S
==> 500012011000.S3S
==> 500014011000.S3S
==> 500028011201.S3S
==> 500135010100.S3S
==> 500143010002.S3S
==> 500142010100.S3S
==> 500144010100.S3S
==> 500006011000.S3S
==> 500007011000.S3S
==> 500008011000.S3S
==> 500009011000.S3S
==> 500010011000.S3S
==> 500011011000.S3S
==> 500012011000.S3S
==> 500014011000.S3S
==> 500016011000.S3S
==> 500134010100.S3S
==> 500028011201.S3S
==> 500129010200.S3S
==> 500072010400.S3S
==> 500135010100.S3S
==> 500143010002.S3S

I need to be as this in new txt file

500007011000.S3S
500011011000.S3S
500016011000.S3S
500072010400.S3S
500136010100.S3S
500022010300.S3S
500007011000.S3S
500008011000.S3S
500019011200.S3S
500008011000.S3S
500009011000.S3S
500012011000.S3S
500014011000.S3S
500028011201.S3S
500135010100.S3S
500143010002.S3S
500142010100.S3S
500144010100.S3S
500006011000.S3S
500007011000.S3S
500008011000.S3S
500009011000.S3S
500010011000.S3S
500011011000.S3S
500012011000.S3S
500014011000.S3S
500016011000.S3S
500134010100.S3S
500028011201.S3S
500129010200.S3S
500072010400.S3S
500135010100.S3S
500143010002.S3S

Thanks so much again for all your kind replies

William

Re: trim off some characters of lines in txt file

Posted: 01 Aug 2017 12:53
by goodywp
Problem solved ! instead of using % I used ! it works! Could anyone tell me why ?

Code: Select all

@echo off 

setlocal enabledelayedexpansion

for /f "delims=" %%i in (sch_repl_list_after.txt) do (

    set line=%%i 

    echo !line:~6, 16! >>removed_str.txt
    
)

Re: trim off some characters of lines in txt file

Posted: 01 Aug 2017 14:16
by penpen
The command line interpreter expands percentage variables (%var%) before executing a command.
So a block ("( command+ )") is executed after the percentage variable was expanded.
Setting the variables value won't affect the result of the earlier percentage expansion.
The exclamation mark variables are expanded right before an atomic command is executed (delayed expansion), so it could expand to the right value, set with a previous atomic command.


penpen

Re: trim off some characters of lines in txt file

Posted: 02 Aug 2017 08:39
by goodywp
Thanks Penpen! How to make the above code works by using % if possible

Re: trim off some characters of lines in txt file

Posted: 02 Aug 2017 09:24
by elzooilogico

Code: Select all

echo !line:~6,16!

can be

Code: Select all

call echo %%line:~6:16%%

note double percent sign to force call evaluate the term inside. but performance will be worst than using delayed expansion (think about a large file to process)

also, if performance is in your mind

Code: Select all

for ... do (
  echo whatever>>somefile.txt
)

will open and close the file every time, where

Code: Select all

>somefile.txt (
  for ... (
      echo whatever
   )
)
will open the file before the block, and close it after, so only one open/close. this may improve performance a lot.

Re: trim off some characters of lines in txt file

Posted: 03 Aug 2017 08:11
by goodywp
Thanks so much elzooilogicofor your detailed explanation with sample. that helps us a lot in terms of why that works that way... :D