Copy text from file A to B at specific locati

November 9, 2009 at 10:03:19
Specs: Windows XP
Hi,
I want create a batch file to copy some
specific(variable) text from a file A.txt to
another file B.txt's specific location (in
between a line at specific location).

How to create this file.....

Please help me.


See More: Copy text from file A to B at specific locati

Report •


#1
November 9, 2009 at 12:03:44
is text in file "A" at a specific line, or do you want text based on the contents of the text (f/e: if line contains "AARDVARK"
insert this line into file B at line 5)

if "A" extraction is context-based, does (or can) the string occur more than once in file A, and if so, do you just want to ignore all but the first occurance?

this will do part of it, rest needs further elucidation of problem.
22 is used as an example constant value.

setlocal enabledelayedexpansion
set /a targ=22
set /a N=0
for /f "tokens=1 delims=" %%a in (B.txt) do (
set /a N+=1
echo %%a >> C.txt
if !N! equ !targ! call :nsrt)
move /y C.txt B.txt
goto ex

:nsrt
:this part needs more information to be
:effective
set text=AARDVARK
find /i "%text%" < A.txt >> B.txt


:ex


Report •

#2
November 9, 2009 at 22:15:01
Hi,
Text is at the specific line (eg. line no. 2), and copy to the spcific line (eg. line no.
4).

for example:
file A contain:
1.Hello
2.My ID is "STAR_10112009_113500(STAR_some date_ some time)"

file B contains:
1.
2.Dear Sir
3.
4.My friend's name is "STAR" and id is "STAR_10112009_113500"

-------

Please do the needful....


Report •

#3
November 10, 2009 at 17:24:14

setlocal enabledelayedexpansion
set /a Atarg=2
set /a Btarg=4
set mid1=My friends name is
set mid2= and id is
set /a N=0
for /f "tokens=1 delims=" %%a in (B.txt) do (
set /a N+=1
echo %%a >> C.txt
:note that this code is set up to INSERT at line4, not to
:REPLACE line 4. to replace, you need an if - else
:arrangement with the echo a>>c statemt in the else clause.
if !N! equ !Btarg! call :nsrt)
move /y C.txt B.txt
goto ex

:nsrt
set /a M=0
:make sure 1 space after 'delims=' just before the quote
for /f "tokens=1-3,4* delims= " %%b in (A.txt) do (
set /a M+=1
if !M! equ !Atarg! (
set line=%%e
set text=_
set x=
call :extract
set x1=!x!
set text=(
call :extract
set x2=!mid1! !x1!" !mid2! !x!"
echo !x2! >> C.txt
: i know there's more elegant/proper method to exit,
:just cant recall it right now. i saw it in these posts...
goto ex))

:extract
set bite=!line:~0,1!
set line=!line:~1!
if "!bite!" equ "!text!" goto ex
set x=!x!!bite!
goto extract

:ex

this should work but i had to transcribe it from the Xp, so there may be errors in the transcription (my Xp is down, no
floppydrive or network access)


Report •

Related Solutions

#4
November 10, 2009 at 22:05:31
not working... :(
is it incomplete??
what is :ex

Please help..


Report •

#5
November 11, 2009 at 00:52:50
i'm sorry, ex is simply a label for the general exit. Try this:
every reference to "goto ex" change it to "goto :ex" in code. my tests worked, but i didn't use the colon. if you get error, please submit any error info next post.
also, the first 4 to 6 lines were just references to your code, and i have deleted them in this re-post. they were comments only. should start with "setlocal" ignore/delete all before.

Report •

#6
November 11, 2009 at 05:14:17
Sir,
I think u did not get my question.

I have 2 text file, A.txt and B.txt.
Some text (Bold) is veriable, and will be change every day.
file A.txt contain:
1.Hello
2.My ID is "STAR_10112009_113500"


file B contains: (all fixed except WXYZ and WXYZ_0000000_111111):

1.
2.Dear Sir
3.
4.My friend's name is "WXYZ" and id is "WXYZ_0000000_111111"

Now, I want to know to code to replace WXYZ with STAR and WXYZ_0000000_111111 with STAR_10112009_113500 randomly (If the contains of file A.txt change, some contains of file B.txt should be changed.)


Please help me...


Report •

#7
November 11, 2009 at 17:15:07
*Some text (Bold) is veriable, and will be change every day.

ah, i did not understand that file A changes. file B only needs line four changed, correct? not anything else in the file, just line 4. File A, only extract from line 2, right?
also:
2.My ID is "STAR_10112009_113500(STAR_some date_ some time)"
here, you had parentheses in the string at file A line 2. Are they not there? or just sometimes there? or always there?
since your latest update doesn't have them, i will take it they're not there for now.

I'll give it one more go based on this:
setlocal enabledelayedexpansion
set /a Atarg=2
set /a Btarg=4
:put space at end of mid1 and mid2, after "is"
set mid1=My friends name is
set mid2= and id is
set /a N=0
for /f "tokens=1 delims=" %%a in (B.txt) do (
set /a N+=1
if !N! equ !Btarg! (
call :nsrt
) else (
echo %%a >> C.txt)
)
move /y C.txt B.txt
goto ex

:nsrt
set /a M=0
:make sure 1 space after 'delims=' just before the quote
for /f "tokens=1-3,4* delims= " %%b in (A.txt) do (
set /a M+=1
if !M! equ !Atarg! (
set line=%%e
set text=_
set x=
call :extract
set x1=!x!
set x=!x!_
set text="
call :extract
set x2=!mid1! !x1!" !mid2! !x!"
echo !x2! >> C.txt
goto ex))

:extract
set bite=!line:~0,1!
set line=!line:~1!
if "!bite!" equ "!text!" goto ex
set x=!x!!bite!
goto extract

:ex

:END OF CODE is at :ex
the above script worked on my system using the data you gave me so there's not much more that i can do for you. If it fails this time, you'll probably have to start a new thread to try to get more capable programmers (i am not very good, the other people are much better at this.) good luck.


Report •

#8
November 14, 2009 at 07:30:03
Oh.. its working...

You are great....
awesome...

Thanks, Thanks a lot......
:)


Report •

#9
November 15, 2009 at 05:27:55
if you can download and use gawk for windows(see my sig), here's a much shorter script.
FNR==NR && FNR==2{
 gsub("\042","",$NF)
 b=$NF;  split($NF,t,"_");  a=t[1]
 next
}
FNR!=NR{
 for(i=1;i<=NF;i++){
  if($i ~ /WXYZ/){    gsub(/WXYZ/,a,$i);   p++  }
  if(p== 2 && $i ~ /WXYZ/) {    $i=b   }
 }
 print
}

output

C:\test>gawk -f test.awk file1  file2
1.
2.Dear Sir
3.
4.My friend's name is "STAR" and id is "STAR_0000000_111111"

GNU win32 packages | Gawk


Report •


Ask Question