Yesterday, I was working at a client site. The site had a Windows server that was isolated from external internet access, and installing new software was prohibited. I was tasked with removing all the
id values in YAML files, setting them to
null. For instance, the input file temp.yaml looks like this:
something id: 4 something else id: 64 next one id: 231 another one id: 34
The target file (result.yaml) that I wanted would look like this:
something id: something else id: next one id: another one id:
The file was large, so removing each
id value manually would be time-consuming. The only tool accessible on that Windows server was the CMD command prompt. Hence, I wrote a simple batch script to complete the task. Create a file called convert.bat in a text editor with the following content:
@echo off for /f "tokens=1* delims=:" %%a in (temp.yaml) do ( if "%%b"=="" ( echo %%a ) else ( echo %%a | find " id" > null && echo %%a: || echo %%a: %%b ) ) > result.yaml
You can replace the text temp.yaml and result.yaml in the script with your target input and output files, respectively. Double-click to execute the script, and you're done!
For those unfamiliar with Batch scripts, here are some basic explanations:
@echo off means to suppress the command prompt display, effectively hiding it.
forloop has some options. The tokens= parameter specifies which numbered items to read from each line (default is 1), and delims= specifies the delimiter character (default is a space).
The %%a and %%b variables are similar to arguments in batch files.
The last line exports the result to the desired file. This simple script saves a lot of time compared to doing the work manually. :)