Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To extract multiple lines with find, trim lead and trail spaces

My previous question was to extract multiple lines from a text files, which is working fine now but it still needs some improve to trim leading and trailing(tricky part) whitespaces/tab spaces and from searching thru Stackoverflow i found many answers and let me combine my final batch-script but from all putting it together it could be not that right.

Details.txt (source file)

line not needed, Copy and help with these command prompt:
line not needed:
(whitespace)(whitespace)some text not needed Copy "c:\.." a b c(white space)
line not needed:
line not needed, Copy and help with these command prompt:
(whitespace)Copy "d:\.." a c c(tab space)
line not needed
(tab space)(tab space)Copy "e:\.." a a c(whitespace)
line not needed

op.txt (Output file)

Copy "c:\.." a b c
Copy "d:\.." a c c
Copy "e:\.." a a c

Let's see the first batch-script. (without trim lead and trail spaces)

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET source="details.txt"
IF EXIST %source% (
  FIND /i "copy " <%source% |FIND "\" >op.txt
) ELSE (
  Exit
)

The combined script to trim lead and trail spaces which gives me two outputs(not big deal). But if we can shorten it both in output and code would be great.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET source="details.txt"
IF EXIST %source% (
  FIND /i "copy " <%source% |FIND "\" >op.txt
  >op2.txt (
    FOR /f "delims=" %%A IN (op.txt) DO (
      SET "ln=%%A"
      SET str=!ln:*Copy=Copy!
      FOR /l %%b IN (1,1,31) DO (
        IF "!str:~-1!"==" " SET str=!str:~0,-1!
        IF "!str:~-1!"=="   " SET str=!str:~0,-1!
      )
      ECHO !str!
    )
  )
) ELSE (
  Exit
)

It actually works in my most situation of source files but I'm sure that this could be not well coded. So i would to ask to improve this batch-script in some ways like output or maybe a shorter/easier version.

Thanks, foxidrive for the start off and helping to solve at my first problem.

Updated version from David (Now with all output)

@ECHO OFF
SETLOCAL
SET "sourceFile=details.txt"
SET "outputFile=opDavid.txt"
@ECHO OFF > "%outputFile%"
IF EXIST "%sourceFile%" FOR /f "delims=" %%A IN ('FIND /i "copy " ^<%sourceFile% ^|FIND "\"') DO CALL :Trim %%A >>%outputFile%
ENDLOCAL
EXIT /b 0

:Trim
ECHO(%*
EXIT /b 0

Sorry for my English, it's poor like my coding

like image 295
Double Quotes Avatar asked Dec 07 '25 21:12

Double Quotes


1 Answers

This will do what you want, but with restrictions.

  • The following "poison" characters must not be in the string: | < > & ^ %

The Trim routine works by using the call command to trim any leading and trailing white spaces.

@ECHO OFF
SETLOCAL
SET "source=details.txt"
SET "output=op.txt"
@echo off > "%output%"
IF EXIST "%source%" FOR /f "delims=" %%A in ('FIND /i "copy " ^<%source% ^|FIND "\"') do call :Trim %%A >> "%output%"
ENDLOCAL
EXIT /b 0

:Trim
echo(%*
EXIT /b 0

Update to answer questions from the comments

  • Restriction Reasons:
    • | < > & will cause the script to crash unless inside quotations or escaped correctly with ^.
    • ^ will be removed from the string unless inside quotations because it is the escape char unless it itself is escaped.
    • % will be removed from the string even inside quotations because it is the variable char unless it itself is escaped %%.
    • Note that each of these character has to be escaped for each time cmd parses them which can become a major pain in the.
    • Batch Special Characters
  • There are other complicated situations which require an understanding of how cmd works. Read These Answers!

NOTE that most of these restrictions can be handled but it requires several more lines of code and then there might still be some restrictions. See My FindAndReplace.bat proof of concept for a look into what it would take.

The quotations surround the variable and value set "var=value" are there to provide special character protection without having to put quotations in the value itself.

  • My General Batch String Rule (And I believe the Batch Community Agrees): Avoid surrounding quotations within variable values and quote variables when used.

This prevents scenarios where a variable has quotations and is in quotations. This below will fail since the

set var="value with spaces"
if "%var%"="value with spaces"
:: this becomes
if ""value with spaces""="value with spaces"

ENDLOCAL closes the scope from the SETLOCAL command, therefore, cleaning up all the changes made to the environment. See This Reference

like image 166
David Ruhmann Avatar answered Dec 09 '25 17:12

David Ruhmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!