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

Time:12-11

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


@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

CodePudding user response:

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 }
)
  • Related