Add character at the end of a line

Microsoft Windows xp professional w/serv...
October 7, 2010 at 16:39:26
Specs: Windows XP
Hey all I hope you can help me.
I have a text file e.g. test.txt
In this file I have an IP address missing the last octet.
So it reads: 10.50.60.

The problem I have is that some come back without the .

So it reads: 10.50.60

I need a batch file to add a . if it doesn't have one to the end of that line.

Cheers


See More: Add character at the end of a line

Report •

#1
October 7, 2010 at 16:51:10
It might help to post a chunk of the file.

Then again, it might not.


=====================================
Helping others achieve escape felicity

M2


Report •

#2
October 7, 2010 at 17:10:01
Hey Mechanix2Go,

The file contains one line.

Looks like this

10.50.60.

That one is fine.

But some are coming out with the line like this

10.50.60

Without the dot.


Report •

#3
October 7, 2010 at 17:17:12
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in (myfile) do (
set S=%%a
set S=!S:~-1!
if !S! neq . (
>> newfile echo.%%a.
) else (
>> newfile echo.%%a
)
)


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
October 7, 2010 at 17:39:11

download sed for windows then do this one liner

C:\test>sed -i -r "s/([^.])$/\1./" file

GNU win32 packages | Gawk


Report •

#5
October 7, 2010 at 17:39:16
Awesome stuff M2, that works adding a dot at the end of that line.

But for those lines that already contain a dot, its adding another one.

Eg

10.50.60..

How would you make it look for a dot at the end of the line, if it doesn't have one add it, if it does then don't.


Report •

#6
October 7, 2010 at 17:43:39
"But for those lines that already contain a dot, its adding another one."

Not here.

Paste in your file.

And tell us the size.


=====================================
Helping others achieve escape felicity

M2


Report •

#7
October 7, 2010 at 19:10:10
Here are the contents of the batch file:

@echo off > new.txt & setLocal enableDELAYedeXpansion
for /f "tokens=* delims= " %%a in (newfile.txt) do (
set S=%%a
set S=!S:~1!
if !S! neq . (
>> new.txt echo.%%a.
) else (
>> new.txt echo.%%a
)
)

So newfile.txt only contains the following line.

10.50.6.

Both files are 1kb

So because the newfile.txt already has a dot after the 6, it's adding another dot.

10.50.6..


Report •

#8
October 7, 2010 at 19:22:38
I mean post the file with the IP.

And the real size. It's not 1KB.


=====================================
Helping others achieve escape felicity

M2


Report •

#9
October 7, 2010 at 19:39:50
@M2, it should be

set S=!S:~-1!

GNU win32 packages | Gawk


Report •

#10
October 7, 2010 at 19:47:04
Call me crazy. Isn't that what I posted?


=====================================
Helping others achieve escape felicity

M2


Report •

#11
October 7, 2010 at 19:48:22

The file size is 10bytes for that newfile.txt

Not sure how to attach a file.


Report •

#12
October 7, 2010 at 19:59:19
I think ghostdog spotted the problem.

I can't imagine why anybody would waste their time typing in a script when you could copy it.

But if you do, it's probably well worth typing carefully.


=====================================
Helping others achieve escape felicity

M2


Report •

#13
October 7, 2010 at 20:03:44
>>Call me crazy. Isn't that what I posted?

yes, its what you posted. I actually addressed to the wrong person. should be @OP and not @M2

GNU win32 packages | Gawk


Report •

#14
October 7, 2010 at 20:36:16
Yeah sorry guys, my fault.

Thanks for the help.


Report •

Ask Question