[Answered]findstr output only text that begins without...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

[Answered]findstr output only text that begins without...

#1 Post by BoQsc » 03 May 2015 05:15

Hey, i'm in need of help.
I want it so every line that is not begining with ;;;===,,, exclude from output file, when i'm doing output.

Problem is, output.txt still contains line marked with ;;;===,,,

Code: Select all

;;;===,,,findstr /v "^;;;===,,," "%~f0" > output.txt
and it shouldn't.

Code: Select all

@echo off

    ;;;===,,,findstr /v "^;;;===,,," "%~f0" > output.txt
pause


Might be nice if someone give me helpful hand.

Output File i want to be must look like this after process:

Code: Select all

@echo off

pause
Last edited by BoQsc on 05 May 2015 06:58, edited 1 time in total.

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: findstr output only text that begins without...

#2 Post by aGerman » 03 May 2015 07:05

Your line doesn't begin with ;;;===,,, but with spaces or tabs :wink:

Regards
aGerman

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: findstr output only text that begins without...

#3 Post by ShadowThief » 03 May 2015 08:40

Isn't whitespace usually ignored, though?

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: findstr output only text that begins without...

#4 Post by aGerman » 03 May 2015 09:08

No. If there are spaces in front of the line I would rather use Options /b, /r, and /c. E.g.:

Code: Select all

;;;===,,,findstr /vbrc:"[ ]*;;;===,,," "%~f0" > output.txt

[ ]* means no, one, or more spaces are matched.

Regards
aGerman

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: findstr output only text that begins without...

#5 Post by BoQsc » 03 May 2015 14:13

Thank you aGerman for explanation, it seems to work now almost perfectly. But there is one problem:
Both of solutions yours and given by me (original) also somehow removes some symbols/text out of Output file that is not expected.

What i'm doing right now is placing binary/.exe file into batch file. And then later on trying to output that binary/exe file.
The output.exe file size is less by 1kb than original.exe and so it is corrupted, can't be launched after all.

Here is source: combine bat with exe

Press Build.bat to build from resources.

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: findstr output only text that begins without...

#6 Post by aGerman » 03 May 2015 14:25

Why do you expect findstr would be able to process binary data? It's a tool to process text.

BTW: What's the reason to merge wget with a batch code? Just use it as is.

Regards
aGerman

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: findstr output only text that begins without...

#7 Post by penpen » 03 May 2015 15:01

@BoQsc:
You cannot cut off some "unneeded inbetween bytes" using findstr because you are changing all folllowing function offsets.
==> The resulting executable is damaged.

@aGerman:
I always thought, that findstr is able to handle any binary data, if you are using cmd.exe in ANSI mode.

An example that depends on that behaviour is:
http://www.dostips.com/forum/viewtopic.php?f=3&t=5326.
Maybe you have to use an appropriate codepage (but i doubt it, else the above example should have produced errors on somebodys pc).

You also could copy binary files using:

Code: Select all

Z:\>findstr "^" "%ComSpec%" > "cmd2.exe"

Z:\>fc /B "%ComSpec%" cmd2.exe
Vergleichen der Dateien cmd.exe und CMD2.EXE
FC: Keine Unterschiede gefunden


penpen

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: findstr output only text that begins without...

#8 Post by BoQsc » 05 May 2015 06:18

@penpen, what if i encode whole binary file into base64.
It seems to me that encoding and decoding works well with certutil. But even encoded binary can't be extracted without damaging encoded code using findstr.
Is there any flags, internal (native) tools that can extract encoded base64 code without damaging it?
I'm almost sure it's possible, but the findstr is not getting right settings to do so, so it cuts something out.
example of the encoded binary (only some part of it) with base64 using certutil:

Code: Select all

TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAA+AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v
dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABFILttAUHVPgFB1T4BQdU+
CDlGPjdB1T7cvho+AEHVPty+GD4DQdU+AUHUPghA1T7cvh4+CEHVPty+Gz4XQdU+
3L4CPiRB1T7cvhw+AEHVPty+GT4AQdU+UmljaAFB1T4AAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAABQRQAAZIYHALFCUFQAAAAAAAAAAPAAIgALAgsAAMICAACuAgAAAAAA
IG4AAAAQAAAAAABAAQAAAAAQAAAAAgAABgADAAYAAwAGAAMAAAAAAADQBQAABAAA
R64FAAMAYMEAABAAAAAAAADADwAAAAAAAAAQAAAAAAAAEAAAAAAAAAAAAAAQAAAA
AAAAAAAAAAB4+AQAgAIAAAAwBQBYhAAAALAEAFA3AAAAAAAAAAAAAADABQBMAQAA
iKMCADgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQbQEAlAAAAAAAAAAAAAAA
APAEAHAIAAD4oAIAYAAAAAAAAAAAAAAAAAAAAAAAAAAudGV4dAAAAOzAAgAAEAAA
AMICAAAEAAAAAAAAAAAAAAAAAAAgAABgLmRhdGEAAABAwQEAAOACAADCAQAAxgIA
AAAAAAAAAAAAAAAAQAAAwC5wZGF0YQAAUDcAAACwBAAAOAAAAIgEAAAAAAAAAAAA
AAAAAEAAAEAuaWRhdGEAAMIoAAAA8AQAACoAAADABAAAAAAAAAAAAAAAAABAAABA
LmRpZGF0AACAAAAAACAFAAACAAAA6gQAAAAAAAAAAAAAAAAAQAAAwC5yc3JjAAAA
WIQAAAAwBQAAhgAAAOwEAAAAAAAAAAAAAAAAAEAAAEAucmVsb2MAAEwBAAAAwAUA
AAIAAAByBQAAAAAAAAAAAAAAAABAAABCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA


I have no idea how to extract encoded base64 binary from batch safely, so i'm asking for help.

Ps. i actually want to place more than one small file into batch. But i'm guessing it might be really hard to do so or even imposible?

Why i want that? I want it because i and others don't want to download those little tools like wget everytime on every new computer.
I want to automate the process for sure. Some tasks batch can't do it self or it's not that simple.
Yes, maybe wget can be replaced with powershell, but it have restrictions and can't be automated that fast... requires for people interuption to enable use of script for the first time... And so on.
Yeah there is work around with vbscripts... But let's be honest, there is no need to tell whole story why i decided choose encoding executable and merge with batch. It is not going to help finding solution for extracting base64 safely.

Difference between original base64 and extractted is only 29 bytes...


Okey, everything seems to work. Thanx everybody who tried to help.
Except for the multifile output.
Last edited by BoQsc on 06 May 2015 08:43, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: [Answered]findstr output only text that begins without..

#9 Post by penpen » 05 May 2015 13:11

The problem with your batch-exe/extraction is that you don't start the binary data at the start of a line, but after ";;;===,,,exit /b".
So the first "line" of the binary file (all up to the first '\n' character, using c++ notation) is cut off.
Because this line starts with ";;;===,,,", and you said in your opening post that you don't want such lines, i called this line "unneeded inbetween bytes" (i thought the irony was obvious - sorry for using it).

This issue stays no matter how the binary data is encoded.

If you don't want to change your "extraction" algorithm, you should ensure the following:
1. If the batch does no end on "\r\n" (c++ notation for carriage return, newline), append it to the batch portion.
2. If the batch ends on multiple "\r\n" delete all except one.
3. The binary portion is disallowed to have ";;;===,,," at the start of a "line".

Then findstr should be able to "restore" the binary data (in any encoding).


penpen

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

Re: [Answered]findstr output only text that begins without..

#10 Post by foxidrive » 05 May 2015 22:52

You can use this batch file to create a script that you can add to your utility batch file:


Launch it like this: Batchfile wget.exe

and it will create wget.exe.decode.txt that you can add to your batch file.


Code: Select all

@echo off
if "%~1"=="" goto :EOF
certutil /encode /f "%~1" "%~1.encoded"

(
echo((
echo(echo(-----BEGIN CERTIFICATE-----
for /f "delims=" %%a in ('find /v /i "CERTIFICATE---" ^< "%~1.encoded"') do echo(echo(%%a
echo(echo(-----END CERTIFICATE-----
echo(^)^>"%~1.decode"
echo(certutil /decode /f "%~1.decode" "%~1"
echo(del "%~1.decode"
)>"%~1.encoded.txt"
del "%~1.encoded"



This is a sample of what it will generate - this code creates the batch file above:

Code: Select all

(
echo(-----BEGIN CERTIFICATE-----
echo(QGVjaG8gb2ZmDQppZiAiJX4xIj09IiIgZ290byA6RU9GDQpjZXJ0dXRpbCAvZW5j
echo(b2RlIC9mICIlfjEiICIlfjEuZW5jb2RlZCINCg0KKA0KZWNobygoDQplY2hvKGVj
echo(aG8oLS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tDQpmb3IgL2YgImRlbGltcz0i
echo(ICUlYSBpbiAoJ2ZpbmQgL3YgL2kgIkNFUlRJRklDQVRFLS0tIiBePCAiJX4xLmVu
echo(Y29kZWQiJykgZG8gZWNobyhlY2hvKCUlYQ0KZWNobyhlY2hvKC0tLS0tRU5EIENF
echo(UlRJRklDQVRFLS0tLS0NCmVjaG8oXilePiIlfjEuZGVjb2RlIg0KZWNobyhjZXJ0
echo(dXRpbCAvZGVjb2RlIC9mICIlfjEuZGVjb2RlIiAiJX4xIg0KZWNobyhkZWwgIiV+
echo(MS5kZWNvZGUiDQopPiIlfjEuZW5jb2RlZC50eHQiDQpkZWwgIiV+MS5lbmNvZGVk
echo(Ig0KDQp0eXBlICIlfjEuZW5jb2RlZC50eHQiICYgcGF1c2UNCg0K
echo(-----END CERTIFICATE-----
)>"Certutil to batch script.bat.decode"
certutil /decode /f "Certutil to batch script.bat.decode" "Certutil to batch script.bat"
del "Certutil to batch script.bat.decode"




Faster version for huge files - if it's ever needed.

Code: Select all

@echo off
if "%~1"=="" goto :EOF
certutil /encode /f "%~1" "%~1.encoded.1"
find /v /i "CERTIFICATE---" <"%~1.encoded.1" >"%~1.encoded.2"

(
echo((
echo(echo(-----BEGIN CERTIFICATE-----
for /f "usebackq delims=" %%a in ("%~1.encoded.2") do echo(echo(%%a
echo(echo(-----END CERTIFICATE-----
echo(^)^>"%~1.decode"
echo(certutil /decode /f "%~1.decode" "%~1"
echo(del "%~1.decode"
)>"%~1.encoded.txt"
del "%~1.encoded.?"

npocmaka_
Posts: 517
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: [Answered]findstr output only text that begins without..

#11 Post by npocmaka_ » 06 May 2015 03:09

In fact you do not need temp file do decode base64 :) :


Code: Select all

@echo off
certutil /decode /f "%~f0" decoded.txt
exit /b 0
-----BEGIN CERTIFICATE-----
ZGVjb2RlZCBiYXNlNjQgc3RyaW5n
-----END CERTIFICATE-----

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

Re: [Answered]findstr output only text that begins without..

#12 Post by foxidrive » 06 May 2015 04:18

npocmaka_ wrote:In fact you do not need temp file do decode base64 :) :


Code: Select all

@echo off
certutil /decode /f "%~f0" decoded.txt
exit /b 0
-----BEGIN CERTIFICATE-----
ZGVjb2RlZCBiYXNlNjQgc3RyaW5n
-----END CERTIFICATE-----


That can be useful - good thinking!

penpen wrote:Then findstr should be able to "restore" the binary data (in any encoding).


That looks like good sleuthing too penpen.

Post Reply