Script to modify a txt file

Hi I want create a script to modify a file text like this, I tried with a batch file but couldn't, can anyone give me advice on which language i should rely on to do this?:

http://esv5.net:80/c/ 
00:1a:79:17:52:ae 
00:1a:79:b3:51:43 
00:1a:79:b0:5f:78 
http://mag.abcdxyz.one/c
00:1a:79:d6:3f:2a 
00:1a:79:2e:e7:e1 
http://magmeg2mig4z.funtogether.xyz:8080/c
00:1a:79:4d:3a:c0 
00:1a:79:ab:3c:39 

to this:

 http://esv5.net:80/c/ 
 00:1a:79:17:52:ae 
 http://esv5.net:80/c/ 
 00:1a:79:b3:51:43 
 http://esv5.net:80/c/ 
 00:1a:79:b0:5f:78 
 http://mag.abcdxyz.one/c
 00:1a:79:d6:3f:2a 
 http://mag.abcdxyz.one/c
 00:1a:79:2e:e7:e1 
 http://magmeg2mig4z.funtogether.xyz:8080/c
 00:1a:79:4d:3a:c0 
 http://magmeg2mig4z.funtogether.xyz:8080/c
 00:1a:79:ab:3c:39 

My script is:

@ECHO OFF
SETLOCAL
rem The following settings for the source directory & filenames are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=C:\Users\dom19\Downloads\file.txt"
SET "filename1=%sourcedir%\q71207091.txt" 

FOR /f "usebackqtokens=1*delims=: " %%b IN ("%C:\Users\dom19\Downloads\file.txt%") DO (
 IF "%%b"=="Server" ECHO %%c
 IF "%%b"=="MAC" ECHO %%c
 IF "%%b"=="http" ECHO %%b:%%c
 IF "%%b"=="00" ECHO %%c|FINDSTR /b "1a:79:" >NUL &IF NOT ERRORLEVEL 1 FOR /f "delims=;" %%e IN ("%%c") DO ECHO %%b:%%e 
) >> "C:\Users\dom19\Desktop\script1.txt" 

After to sort the results failed

@dom1987 , hi, sorry cannot help wrt a pure windows (?powershell?) implementation, but if you have (or can install *nix tools - cygwin or equivalent ) a simple awk script will do the job

based on the sample data you provided ....

cat script.awk
/^http/{hdr=$0;next}
{ print hdr; print $0}
$
$ awk -f script.awk in
http://esv5.net:80/c/ 
00:1a:79:17:52:ae 
http://esv5.net:80/c/ 
00:1a:79:b3:51:43 
http://esv5.net:80/c/ 
00:1a:79:b0:5f:78 
http://mag.abcdxyz.one/c
00:1a:79:d6:3f:2a 
http://mag.abcdxyz.one/c
00:1a:79:2e:e7:e1 
http://magmeg2mig4z.funtogether.xyz:8080/c
00:1a:79:4d:3a:c0 
http://magmeg2mig4z.funtogether.xyz:8080/c
00:1a:79:ab:3c:39 

i also understand there's a bash shell for windows, i have no windows experience per se, so , as always please test and test again !!!!

Alternatively look at installing https://en.m.wikipedia.org/wiki/Windows_Subsystem_for_Linux ( thanks @vgersh99 )

the following script might do the job

$cat script.bash
while read line
do 
        if [[ ( "${line:0:5}" == "http:" ) ]]
        then
                hdr="${line}"
        else
                printf "%s\n%s\n" "${hdr}" "${line}"
        fi
done < sample.data
$
$ script.bash
http://esv5.net:80/c/
00:1a:79:17:52:ae
http://esv5.net:80/c/
00:1a:79:b3:51:43
http://esv5.net:80/c/
00:1a:79:b0:5f:78
http://mag.abcdxyz.one/c
00:1a:79:d6:3f:2a
http://mag.abcdxyz.one/c
00:1a:79:2e:e7:e1
http://magmeg2mig4z.funtogether.xyz:8080/c
00:1a:79:4d:3a:c0
http://magmeg2mig4z.funtogether.xyz:8080/c
00:1a:79:ab:3c:39
$

Or get WSL.

1 Like

Is perfect thanks, now i want convert result from txt to json format, how I can do?
My result is this:

http://esv5.net:80/c/ 
00:1a:79:17:52:ae 
http://esv5.net:80/c/ 
00:1a:79:b3:51:43 
http://esv5.net:80/c/ 
00:1a:79:b0:5f:78 
http://mag.abcdxyz.one/c
00:1a:79:d6:3f:2a 
http://mag.abcdxyz.one/c
00:1a:79:2e:e7:e1 
http://mag.abcdxyz.one/c
00:1a:79:7b:9e:27 
http://mag.abcdxyz.one/c
00:1a:79:a4:67:12

I want convert in json

[
  {
    "Name": "user1",
    "Poster": "logo.png",
    "Address": "http://esv5.net:80/c/",
    "Mac": "00:1a:79:17:52:ae"
  },
  {
    "Name": "user2",
    "Poster": "logo.png",
    "Address": "http://esv5.net:80/c/",
    "Mac": "00:1a:79:b3:51:43"
  },
  {
    "Name": "user3",
    "Poster": "logo.png",
    "Address": "http://esv5.net:80/c/",
    "Mac": "00:1a:79:b0:5f:78"
  },
  {
    "Name": "user4",
    "Poster": "logo.png",
    "Address": "http://mag.abcdxyz.one/c",
    "Mac": "00:1a:79:d6:3f:2a"
  },
  {
    "Name": "user5",
    "Poster": "logo.png",
    "Address": "http://mag.abcdxyz.one/c",
    "Mac": "00:1a:79:2e:e7:e1"
  },
  {
    "Name": "user6",
    "Poster": "logo.png",
    "Address": "http://mag.abcdxyz.one/c",
    "Mac": "00:1a:79:7b:9e:27"
  },
  {
    "Name": "user7",
    "Poster": "logo.png",
    "Address": "http://mag.abcdxyz.one/c",
    "Mac": "00:1a:79:a4:67:12"
  }
] 

where is the 'Name' and the 'Poster' tag data coming from?
They're not in the sample input file you've provided.
Have you tried anything to achieve your objective yet?

I want to create 'Name' and the 'Poster' tags by default where Name is ascending and 'Poster' is always the same path for example "C: \ Users \ name \ Document \ logo.png"
To achieve my object not so which program to use. do you have a suggestion?
Sorry for my bad English

Yes, you can modify the awk code provided by @munkeHoller in post #2 to format the output for a given url/mac pairs to create a JSON output.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.