Append data to end of first line

Microsoft Windows xp professional w/serv...
August 3, 2010 at 17:32:22
Specs: Windows XP
Hey, first time poster, love this forum, very helpful.

I have what seems to be a simple one but is driving me insane and I now conceded and need expert help.

I have a simple text file with one line of data an IP address something like this...

10.50.23.

Which is missing the last octet, which is fine, thats what I want.

Now i need a batch file to simply enter a number to the end of that.

My problem is that that text file comes with a space after the . and it also has a second blank line. So when I do some thing simple like echo 7 >> thattextfile.txt

It comes out as:

10.50.23.
7

argh!!!

Please help!


See More: Append data to end of first line

Report •


#1
August 3, 2010 at 18:26:25
download sed for windows then do this

C:\test>sed "s/[ \t]$/400/" file
10.50.23.400

and yes, you can run it on *nix as well.

GNU win32 packages | Gawk


Report •

#2
August 3, 2010 at 18:27:14
Weeelllll, I have a solution for you, but it requires that you download Swiss File Knife (SFK)

Place it either in your System32 folder, OR in the same folder as your batch file.

Here's the final code:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET /p entry=Enter the final octet:

FOR /f "tokens=4 delims=." %%a IN (test1.txt) DO (
	SFK replace "P:\a\t\h\FileName.txt" "/%%a/%entry%/" -yes>Nul
)
PAUSE

Hope it helps, but ghostdog's solution might be simpler.


Report •

#3
August 3, 2010 at 19:00:18
Geez you guys are quick...

Here's my problem. I have a windowz (yay) machine, *nix would have been perfect, sed would have made life in general so much more enjoyable right now.

The batch file sits on a thin client, so installing any sort of thirdparty software is a no go. So I'm leaning towards Matt123's solution because I can copy a single file across and delete it later once the command is complete.

Matt123 I tried running that batch file but it comes up with

7
The system cannot find the file specified.

Do I have to specify the path explicitly? The batch file and the txt file are in the same folder, so it shouldn't be an issue right?


Report •

Related Solutions

#4
August 3, 2010 at 19:46:04
Downloading sed and installing it is just one time. Its just an exe file and you can bring to any machine you want without installing a 2nd time. I am not suggesting you download it using the thin client. Just do it somewhere else and bring over to where you are going to transfer the tool. Note, Matt's solution also require a download. that's no different from mine.

GNU win32 packages | Gawk


Report •

#5
August 3, 2010 at 19:56:39
I would recommend you use ghostdog's solution. Both of ours requires third party software, and his is a one liner. Do the same with sed as you would have with SFK (where to put the file, not the commands).

However, if you are going to still use mine, yes you will need to add a path, sorry for not being clear. See myedited post.


Report •

#6
August 3, 2010 at 20:00:06
Yeah sorry ghostdog, didn't delve deeply enough into the sed executable... I have now though, grabbed the contents of the bin file some odd 1.5m or so, and chucked them into sys32.

I've tried and tested out your solution and its perfect.

Thanks both for all your help!! Legends!


Report •

#7
August 4, 2010 at 11:08:11
Or, more simply:

==========================
@echo off & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in (thefile) do (
set S=%%a
goto :done
)
:done
> newfile echo.!S!77

:: where 77 is the last octet


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

M2


Report •

#8
August 4, 2010 at 15:36:43
Hey M2, that worked, although there is a space directly in front of the 77 so it came out as 10.50.34. 77

Also, kinda on the same topic.

I have another txt file, about a line long, which contains the line "IP_xx.xx.xx.xx"
How would you go about searching out that line and replacing it with IP_10.50.34.77, for example? And for a bit of fun, the last octet isn't always two characters, there are some which are three, so it'll need to cater for IP_10.50.34.100 etc


Report •

#9
August 4, 2010 at 17:41:57
you have seen what sed can do right?

C:\test>more file

asdfjas;fkasfas df
sadfasdfjsadfasdflasjf asfasf  IP_xx.xx.xx.xx  a slafjaslfjaslf

C:\test>sed "s/IP_.[^ \t]*/IP_10.10.10.200/" file

asdfjas;fkasfas df
sadfasdfjsadfasdflasjf asfasf  IP_10.10.10.200  a slafjaslfjaslf

GNU win32 packages | Gawk


Report •

#10
August 4, 2010 at 21:49:19
Hey ghostdog, thanks for that, the power of sed is awesome.

How would I get the IP address into that sed command line to begin with though?

Just say I have a text file with that 10.10.10.200 address only.

Would I use another sed command?


Report •

#11
August 4, 2010 at 23:10:11
just put your variable name inside. eg

set ip=10.111.11.1.
sed "s/IP_.[^ \t]*/IP_%ip%/" file

GNU win32 packages | Gawk


Report •

#12
August 5, 2010 at 00:53:19
oops... had a senior moment there.

===================================
@echo off & setLocal enableDELAYedeXpansion

for /f "tokens=1 delims= " %%a in (thefile) do (
set S=%%a
goto :done
)
:done
> newfile echo.!S!321

:: where 321 is the last octet
===================================
As to your new requirement.

"I have another txt file, about a line long, which contains the line "IP_xx.xx.xx.xx" "

Not precise, but this should do it regardless of number if lines, if only one line containg IP_.

=========================================
@echo off & setLocal EnableDELAYedeXpansion

if %1'==' echo last octet? && goto :eof

for /f "tokens=1-3 delims=. " %%a in ('find "IP_" ^< IPfile') do (
> newfile echo.%%a.%%b.%%c.%1
)


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

M2


Report •

#13
August 5, 2010 at 16:56:31
Guys thanks for all your help so far.

Its still not 100% though. I'm guessing because the file I'm using to import the IP address into has invalid characters or something??

So here is a more detailed explanation of what I'm trying to do.

I have two files.

File a.txt contains a single line of an IP address, which can be 10.50.45.10, for example.

Then I have another txt file, b.txt, containing the line:

rundll32 printui.dll,PrintUIEntry /if /b "Lexmark T654" /f C:\FrontCounterPrinter\LMACLC40.inf /r "IP_10.50.33.7" /m "Lexmark T654" /Z

Where it has IP_10.50.33.7, I need that replaced with the IP from a.txt. So it should read:

rundll32 printui.dll,PrintUIEntry /if /b "Lexmark T654" /f C:\FrontCounterPrinter\LMACLC40.inf /r "IP_10.50.45.10" /m "Lexmark T654" /Z

I hope this makes better sense, I appreciate all your help so far.

Cheers


Report •

#14
August 6, 2010 at 03:52:43
I'm glad we got the real requirement before burning through another dozen msgs.

=====================================

@echo off & setLocal EnableDELAYedeXpansion

set /p I=< a.txt

for /f "tokens=* delims= " %%a in (b.txt) do (
call :sub1 %%a
)
> newfile echo.!S!

goto :eof

:sub1

  :loop
    echo %~1 | find "IP_" > nul
      if errorlevel 1 (
        set T=%1
        ) else (
        set T="IP_!I!"
      )

    if not defined S (
      set S=!T!
      ) else (
      set S=!S! !T!
    )

    if '%2' neq '' (
      shift
      goto :loop
    )

goto :eof


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

M2


Report •

#15
August 8, 2010 at 17:02:37
Sorry for the multitude of posts M2.

The newfile output that i'm getting with that code is:

"IP_10.60.70.7" "IP_10.60.70.7"

Are you getting the same output as me?


Report •

#16
August 8, 2010 at 18:03:16
I get:

rundll32 printui.dll PrintUIEntry /if /b "Lexmark T654" /f C:\FrontCounterPrinter\LMACLC40.inf /r "IP_10.50.45.10" /m "Lexmark T654" /Z


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

M2


Report •

#17
August 8, 2010 at 20:09:50

Yeah actually you're right.

Works fine on my proper XP machine, but on the thin client running windows embedded, it must be missing dll's or something.


Report •

Ask Question