Write from two files to one file

Microsoft Windows xp professional w/sp2
April 8, 2010 at 09:18:20
Specs: Win XP
I have two files:
machine.txt & IP.txt

name.txt has two columns that are space delimited. the first column is a machine name and the second column is the room number.
example:
machine1 23
machine2 10
machine3 2
etc.
Age.txt has one column and is an age.
example:
192.168.1.1
192.168.1.2
192.168.1.3
etc.

I need to write to a new file named MachineIP.txt that has two columns and is space delimited. Column one would be the IP address from IP.txt and column two would be the machinename from machine.txt (The second column from machine.txt is not needed.). The MAchine IP file would look like the following:
192.168.1.1 machine1
192.168.1.2 machine2
192.168.1.3 machine3
etc.

I am a noob,and have spent hours trying to figure this out. Any help would be appreciated.


See More: Write from two files to one file

Report •


#1
April 8, 2010 at 10:47:23
@echo off > newfile & setLocal enableDELAYedeXpansion

set N=
for /f "tokens=1 delims= " %%a in (machine.txt) do (
set /a N+=1
set M!N!=%%a
)

set N=
for /f "tokens=1 delims= " %%a in (IP.txt) do (
set /a N+=1
set I!N!=%%a
)

for /L %%i in (1 1 !N!) do (
>> newfile echo.!I%%i! !M%%i!
)


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

M2


Report •

#2
April 8, 2010 at 19:32:16
download gawk for windows(see my sig), and then try this one liner

C:\test>gawk "{getline ip<\"ip.txt\";print ip,$1}" machine.txt
192.168.1.1 machine1
192.168.1.2 machine2
192.168.1.3 machine3

GNU win32 packages | Gawk


Report •

#3
April 10, 2010 at 03:18:50
Jim, is the example really like it is :

192.168.1.1 machine1
192.168.1.2 machine2
192.168.1.3 machine3

or is the reality more like this:

192.168.1.1 xyz344rf
192.168.1.2 user12
192.168.1.3 desert99

Asking question because if you really have nice numbering (like in the first case) listing down the names in a file is totally unnecessary, as all mappings are :

192.168.1.X machineX


Report •

Related Solutions

#4
April 10, 2010 at 05:13:32
Sure, but the merge approach works regardless.


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

M2


Report •

#5
April 10, 2010 at 07:19:34
I have similiar problem
I have one work that need to open 10 files and process line by line,combine them together, i.e
file 1
...
file 10
each files have same number of lines,each matching line have same first 6 columns but different 7 column, final report is to keep first 6 columns, and join 7 column
example:
c1,c2,c3,c4,c5,c6 file1 c7, file c7 ....file 10 c7
whether your guys example works or not


Report •

#6
April 10, 2010 at 07:32:17
Probably needs a new thread with a few treal file names and a few lines of each.


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

M2


Report •

#7
April 11, 2010 at 03:22:22
I don't quite understand Response 5, but yes indeed, a new thread may be better, and try to better explain the question. It looks something simple, but I don't understand the question in detail, so I can't help.

Report •

#8
April 11, 2010 at 03:28:03
> Sure, but the merge approach works regardless.
>

People often mistake running code with good code ... but if it does the trick, and you have the expected result, it's OK for us.


Report •


Ask Question