#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Howto search and replace text files?


    Hello,

    I have 2 text files like this:

    Text file 1 contains:
    Code:
    TXT     107     
    LN      0025.0  
    A       0025.1  
    A       2036.0  
    S       0337.0  
    LN      0025.1
    Text file 2 contains:
    Code:
    WK      = 0024.3        = werkschakelaar          
    NSIN    = 0025.0        = netschakelaar in
    GSIN    = 0025.1        = gen.schakelaar in
    BL      = 0025.2        = blokkeerschakelaar
    SYN1    = 0025.3        = omschak.met synchronisatie
    SYN2    = 0026.0        = synchronisatie kommando
    STNS    = 0026.1        = storing netschakelaar
    I want to do a search and replace on text file 1 to get this:
    Code:
    TXT     107     
    LN      NSIN    = 0025.0        = netschakelaar in  
    A       GSIN    = 0025.1        = gen.schakelaar in  
    A       2036.0  
    S       0337.0  
    LN      GSIN    = 0025.1        = gen.schakelaar in
    How should I do this? Is there a program available for this? If not anyone can think of a simple algoritme to get it done.

    Thanks.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,612
    Rep Power
    4247
    Yes, you could do this in C or C++, but it might be faster to write the code out in perl or python or some such scripting language which has good string functions. What OS and compiler do you have?

    The best way to do this is to read file 2 into a hash table first, using the numbers as the hash keys. Then read file 1, one line at a time and check the hash to see if you have a key match. If so, output the hash value, otherwise output the line alone.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    I have a win7 OS and a C and C++ compiler. I know a bit of C and C++, but haven't written a C/C++ program in a long time.

    Thanks for the suggestion. I will look into it.

IMN logo majestic logo threadwatch logo seochat tools logo