How can I merge two txt files

November 26, 2010 at 06:58:17
Specs: Windows XP
Hi,
I have two txt files. A.txt and B.txt. In A.txt, I want to replace 3rd, 6th, 9th,...300th (multiplier of 3) lines with same lines in B.txt.

example :
a.txt detail

car
apple
window
disk

b.txt detail
1
3
2
5

new merged.txt file detail
car
apple
2
disk

is it possible to create a script to do that.

thanks.


See More: How can I merge two txt files

Report •

#1
November 26, 2010 at 13:00:06
This bash script should do it:

#!/bin/bash

intvar=0
while read line
do
   ((intvar+=1))
   # every third line, grab a line from the other file:
   if [[ $(($intvar % 3)) -eq 0 ]]
   then
      echo $(sed -n ''"${intvar}"'p' b.txt)
   else
      echo "$line"
   fi
done < a.txt


Report •

#2
November 26, 2010 at 13:31:58
I think nails has a script for everything!

I now know he has a script to find out when people ask questions about scripts and generate a response by brainwaves over IP.

Why did it take me over a year to phone in a problem to ATT?


Report •

#3
November 26, 2010 at 16:25:14
@echo off > merged.txt & setLocal enableDELAYedeXpansion

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

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

for /L %%i in (3 3 !N!) do (
set A%%i=!B%%i!
)

for /L %%j in (1 1 !N!) do (
echo.!A%%j!
) >> merged.txt


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
November 27, 2010 at 08:12:02
Jefro:

Thank you for the kind words. It's much appreciated.

Nails


Report •

Ask Question