Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch File to insert text after 2 words & find a word approx & replace it  (Read 6009 times)

0 Members and 1 Guest are viewing this topic.

chandru630

  • Guest
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


Salmon Trout

  • Guest
Why?

chandru630

  • Guest
I have to complete a task like this. So, i need batch file for this task.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
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.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
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.

Lemonilla



    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
You can do this with a for loop, the word "are" is in the same spot in both lines.
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

chandru630

  • Guest
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.


foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
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"

briandams



    Beginner

    Thanked: 2
    • Experience: Guru
    • OS: Unknown
    you can use awk for windows

    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)

    briandams



      Beginner

      Thanked: 2
      • Experience: Guru
      • OS: Unknown
      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 .

      chandru630

      • Guest
      Re: Batch File to insert text after 2 words & find a word approx & replace it
      « Reply #10 on: January 27, 2014, 03:10:56 AM »
      hi Briandams,

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