Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to echo txt file line by line to specific format with batch?

I have a txt file like this:

3FPC1 00:12:34:56:78:90 192.168.6.1

3FPC2 00:12:34:56:78:91 192.168.6.2

I know how to read txt file line by line by using the following script

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


@echo off
for /F "tokens=*" %%A in (macip.txt) do echo %%A
pause

But I want to covert content to specific format like:

host 3FPC1 {  
  hardware ethernet 00:12:34:56:78:90;
  fixed-address 192.168.6.1;
}
host 3FPC2 {
  hardware ethernet 00:12:34:56:78:91;
  fixed-address 192.168.6.2;
}

How can I do it?Thanks

>Solution :

You are almost there, tell FOR to parse the line just read.

Read HELP FOR and, instead of "tokens=*", use "tokens=1,2,*" for example

So you can

for /F "tokens=1,2,*" %%A in (macip.txt) do (
  echo host %%A {
  echo.   hardware ethernet %%B;
  echo.   fixed-address %%C;
  echo }
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading