Computer Hope

Microsoft => Microsoft DOS => Topic started by: chandru630 on January 23, 2014, 05:24:17 AM

Title: Batch File to insert text after 2 words & find a word approx & replace it
Post by: chandru630 on January 23, 2014, 05:24:17 AM
Hi All,


     I want a batch file to do a task mentioned below:

input.txt

hi how you?
123 123 123

output

hi how are you?
123 123 are 123

Title: Re: Batch File to insert text after 2 words & find a word approx & replace it
Post by: Salmon Trout on January 23, 2014, 05:50:43 AM
Why?
Title: Re: Batch File to insert text after 2 words & find a word approx & replace it
Post by: chandru630 on January 23, 2014, 09:30:47 PM
I have to complete a task like this. So, i need batch file for this task.
Title: Re: Batch File to insert text after 2 words & find a word approx & replace it
Post by: foxidrive on January 24, 2014, 12:26:05 AM
You could get a batch file to solve your fake data, and it may not work on the real data due to character sets and special characters and numbers of characters.

If you show the actual data, replacing some characters to hide any confidential words but not changing the length of the words, then you might find people more willing to have look.
Title: Re: Batch File to insert text after 2 words & find a word approx & replace it
Post by: Squashman on January 24, 2014, 07:47:54 AM
Ah, just link them to Repl and Findrepl usage threads on dostips.  Not that they would be able to figure out how to use them.

Personally pretty easy to do this with vbscript.  I like the replace option in Vbscript.
Title: Re: Batch File to insert text after 2 words & find a word approx & replace it
Post by: Lemonilla on January 24, 2014, 03:15:37 PM
You can do this with a for loop, the word "are" is in the same spot in both lines.
Title: Re: Batch File to insert text after 2 words & find a word approx & replace it
Post by: chandru630 on January 25, 2014, 02:21:03 AM
Hi All,

     I am writing the sample for my batch file.


a text file contains the following data


#define Symbol_Text Characters ( s_textW100H20A, 1, 2)
#define Graphic_Image Rect ( s_ImageW150H32A, 14, 222)
#define Icon_shape position ( s_iconW88H40A, 41, 25)
#define calculate_total msize ( s_ImageW66H66A, 11, 72)

like this a file contains more than 100's of lines.


the output should be like this:

#define Symbol_Text "New Macro" Characters ( s_textW100H20A, 1, 2)
#define Graphic_Image "New Macro" Rect ( s_ImageW150H32A, 14, 222)
#define Icon_shape "New Macro" position ( s_iconW88H40A, 41, 25)
#define calculate_total "New Macro" msize ( s_ImageW66H66A, 11, 72)


Pls help me to do this task. Thanks in advance.

Title: Re: Batch File to insert text after 2 words & find a word approx & replace it
Post by: foxidrive on January 25, 2014, 03:13:56 AM
This uses a helper batch file called `repl.bat` - download from:  https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

Code: [Select]
@echo off
type "file.txt"|repl "(.* .*) (.* \(.*)" "$1 \qNew Macro\q $2" x >"Newfile.txt"
Title: Re: Batch File to insert text after 2 words & find a word approx & replace it
Post by: briandams on January 25, 2014, 10:33:16 AM
you can use awk for windows (http://gnuwin32.sourceforge.net/packages/gawk.htm)

Code: [Select]
C:\> type myFile.txt
#define Symbol_Text Characters ( s_textW100H20A, 1, 2)
#define Graphic_Image Rect ( s_ImageW150H32A, 14, 222)
#define Icon_shape position ( s_iconW88H40A, 41, 25)
#define calculate_total msize ( s_ImageW66H66A, 11, 72)

C:\>awk "{$3=$3\" \042New Macro\042\"}1" myFile.txt
#define Symbol_Text Characters "New Macro" ( s_textW100H20A, 1, 2)
#define Graphic_Image Rect "New Macro" ( s_ImageW150H32A, 14, 222)
#define Icon_shape position "New Macro" ( s_iconW88H40A, 41, 25)
#define calculate_total msize "New Macro" ( s_ImageW66H66A, 11, 72)
Title: Re: Batch File to insert text after 2 words & find a word approx & replace it
Post by: briandams on January 25, 2014, 10:38:05 AM
This uses a helper batch file called `repl.bat` - download from:  https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

Code: [Select]
@echo off
type "file.txt"|repl "(.* .*) (.* \(.*)" "$1 \qNew Macro\q $2" x >"Newfile.txt"

nah, i wouldn't go for a regex solution for this case. A simple DOS for loop, get the 3rd token and concatenate the new string is better and more readable .
Title: Re: Batch File to insert text after 2 words & find a word approx & replace it
Post by: chandru630 on January 27, 2014, 03:10:56 AM
hi Briandams,

   its works for me...Thanks you so much for your reply..