Home > front end >  Is there a way to reproduce SED for string replacement using the Pipe Operator in Batch?
Is there a way to reproduce SED for string replacement using the Pipe Operator in Batch?

Time:01-05

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.

CodePudding user response:

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

CodePudding user response:

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}).

CodePudding user response:

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

  • Related