1

I have a text file having the string:

pub: 
04:d6:b6:f2:98:ff:94:d8:3c:36:ad:5f:86:40:aa:
d1:5a:e1:87:5d:55:9d:ad:8b:2b:fc:18:e7:bb:47:
7f:9f:9a:62:c6:19:3a:9e:65:62:4e:5e:98:6d:db:
0e:7d:f9:22:a3:ca:cb:12:b2:ed:eb:14:0c:b3:31:
59:02:17:6d:6a

I need to remove the 04 from the beginning of this and also remove the ':' from between the characters and print it like this, in a single line:

d6b6f298ff94d83c36ad5f8640aad15ae1875d559dad8b2bfc18e7bb477f9f9a62c6193a9e65624e5e986ddb0e7df922a3cacb12b2edeb140cb3315902176d6a

How can I do that using Windows commands?

5
  • 1
    there is "Windows Batch" (built in) and "Bash for Windows" (third-party "unix"-add-on).What do you use? Commented Apr 19, 2018 at 5:47
  • 1
    What about pub:, do you need to remove that too? Commented Apr 19, 2018 at 6:48
  • I use Windows Batch. And yes, pub: has to be removed. Commented Apr 19, 2018 at 6:56
  • Is that the only string, (entire content), of the text file? Is it Unicode or ASCII? with CR, LF, or CRLF line endings? Commented Apr 19, 2018 at 7:53
  • Have you tried anything on your own? Remember that StackOverflow is not a free code writing service! Commented Apr 20, 2018 at 7:24

2 Answers 2

6

quite straightforward:

@echo off
setlocal enabledelayedexpansion
set "first=true"
(for /f "eol=p delims=" %%a in (input.txt) do (
  set "line=%%a"
  if defined first (set "line=!line:~2!" & set "first=")
  <nul set /p ".=!line::=!"
))>output.txt

Read the input file line by line.
Use a flag (first) to check for first line and remove the first two chars.
Remove the colons and
use <nul set /p to write without a line break.

Edit as it turned out in comments, your file has actually just one long line. This changes the way for processing to:

for /f %%a in (input.txt) do set "line=%%a"
set "line=%line:pub:04:=%"
set "line=%line::=%"
echo %line%

Note: variables can just hold a certain amount of data, so this apporach will fail, when the line exceeds this limit.

Sign up to request clarification or add additional context in comments.

2 Comments

Instead of eol=p I would use skip=1, but that seems to be a matter of taste due to poor specification...
yes, there is more than one way to skin a cat. Each has it's own advantages and disadvantages. I choosed this way, because skip does skip regardless of the content.
2

Your question is pretty confusing. You first say: "I have a text file having the string:", but the example data have six lines that could be taken as six strings, so at this point we have no idea of what the real data format is. Perhaps a single long line that you write here in six parts?

Next, you said "I need to remove the 04 from the beginning of this", but what happen if the data have not a 04 at the beginning? Perhaps you want to remove the first element even if it is not a 04?

In this way, we must assume several points in order to try to write a working solution.

The Batch file below read a file with several lines, remove the first line (even if it does not contain pub:), and remove the first colon-separated element (even if it is not 04):

@echo off
setlocal EnableDelayedExpansion

rem Read all lines, excepting the first one:
set "string="
for /F "skip=1" %%a in (input.txt) do set "string=!string!%%a"

rem Remove the first element:
set "string=%string:*:=%"

rem Show the rest, removing colons:
echo %string::=%

In this code there are other assumptions that are implicit in the way the commands work, like lines that does not contain spaces nor exclamation marks. Of course, if the real data file have a different format, this program will fail...

4 Comments

It is actually a single long line split into 6 parts. And the first colon separated element is always 04 in this case.
Your "explanation" just confuses the things even more: text files contain lines! (not parts nor strings). Did your file contain a single long line or contain six lines? Anyway, I think my code fullfill your requirements and produce the right output. Isn't it?
It has Six lines. And yes, your code has given the right output! Thanks!
@AkhilKintali please be precise. One line or several lines do make a difference. Note: both answers (mine and this one) will fail, when the line(s) is (are) too long, as there is a limit on how much a variable can store.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.