1

I have the below command that produces a SHA256 hash string that I am using within a FOR IN loop with Batch. I've noticed that differences in versions of the certutils results in some outputs being segmented with spaces "ab 7e eg et cc..." instead of a continuous string. I'm looking for a way to update my script to remove the spaces within an additional pipe operation. SED unfortunately does not work natively within Batch and is outside my scope for this.

CertUtil -hashfile SomeFile.txt sha256 | findstr /v "hash" | sed s/ *//g

I've tried using the set str=%str: =% however I haven't gotten the syntax to work properly within my larger script. I am hoping to work within an additional pipe operator step instead of including variables if at all possible.

3 Answers3

3

As per my earlier comment, in your previous question:

@(For /F "Delims=" %%G In ('%SystemRoot%\System32\certutil.exe -HashFfle "SomeFile.txt" SHA256 2^>NUL ^| %SystemRoot%\System32\find.exe /V ":"') Do @Set "hash=%%G" & SetLocal EnableDelayedExpansion & For %%H In ("!hash: =!") Do @EndLocal & Echo=%%~H) & Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
  • If I could borrow some more of your wisdom, I'm trying to apply this bit of code with another part of my script and Batch is just not my expertise. I'm trying to incorporate FINDSTR to check has results against a control file: `FOR /f "delims=" %%b IN (\\filedirectory\*) DO ( FOR /f %%y IN ('certutil -hashfile "%%b" SHA256 ^| find /V ":"') do ( findstr /x "%%y" "HashControlFile.txt" > NUL IF ERRORLEVEL 1 ( `ECHO "%%b" NOT found ) ELSE (` `ECHO del "%%b" )))` – SomekindaRazzmatazz Jan 04 '23 at 19:52
  • Start a new question, and please make sure, that along with your code, it contains the content of `HashControlFile.txt`. – Compo Jan 04 '23 at 20:14
  • I have done as such [here](https://stackoverflow.com/questions/75032669/how-to-iterate-through-for-in-outputs-with-if-statements-in-windows-batch) – SomekindaRazzmatazz Jan 06 '23 at 15:24
-2

Not familiar with CertUtil or findstr, but if all you want is to remove spaces from lines of text like "ab 7e eg et cc..." then the following will work (The forward slash escapes a space in sed)

echo "ab 7e eg et cc" | sed 's/\ //g'

ab7eegetcc

  • 1
    *"SED unfortunately does not work natively within Batch and is outside my scope for this."* – Zak Jan 04 '23 at 18:09
-2

Based on Replace one substring for another string in shell script, you could use:

echo 'a b c' | while read line; do echo ${line// /}; done

The underlying feature (shell parameter expansion) is documented in https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion (search for ${parameter//pattern/string}).

  • Unfortunately I'm working with Windows Batch and not Bash. I tried implementing the same code either way into my Batch script but unfotunately it did not read correctly. – SomekindaRazzmatazz Jan 04 '23 at 18:27
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 04 '23 at 22:39
  • @SomekindaRazzmatazz It wouldn't hurt if you narrow down the question to WINDOWS batch, to make your intentions more clear. – Andreas Meyer Jan 05 '23 at 10:39