locked
Find text between 2 Strings and replace it. RRS feed

  • Question

  • Hello,

    I am new to PowerShell scripting.

    I need help to do the following;

    - I have an original file (let me call it File 1) that has following contents;

    ---------

    File 1.dat

    --------

    #
    #
    #           CHICAGO                   7030
    #           CAIRO                       7032
    #           CHAMPAIGN               7050
    #           FLINT                        7023
    #
    #
    #
    #
    #
    #
    #    CHICAGO  7030
    #
    #ID,SITEID,SITENAME
    204608,348140,NORTH GREEN
    204761,348200,NORTH BLUFORD
    204816,348245,BLUFORD
    204818,348245,BLUFORD
    #
    # CAIRO  7032

    #ID,SITEID,SITENAME
    201448008,343235,BUDA
    201448309,343246,BUDA
    306375,343110,FILLMORE
    306376,343110,FILLMORE
    306394,343105,FILLMORE
    #
    #
    # CHAMPAIGN                      7050
    #
    #ID,SITEID,SITENAME
    56637116,341620_2408,240_95_IS
    57178058,341275_1655,165_50_IS
    57180467,341390_1768,176_78_IS
    #
    #

    There is another file ( file 2 ) which is updated by a Web Service which basically updates data of a Division.

    --------------

    File 2.dat

    -------------

    #
    #           CHICAGO                   7030
    #           CAIRO                       7032
    #           CHAMPAIGN               7051
    #           FLINT                        7023
    #
    #
    # CHAMPAIGN                      7051
    #
    #ID,SITEID,SITENAME
    newdata,newdata,newdata
    newdata,newdata,newdata
    newdata,newdata,newdata
    #

    - I want to update/replace the existing data that is present in File 1 with the new data in File 2 - with the Division that is updated in File 2. Basically copy the contents from File 2 to File 1 at the location where Division name is (# CHAMPAIGN 7050), also update the Division version (from 7050 to 7051) at two places.

    Any suggestions would be most welcome.

    -Thanks

    Matt

    • Moved by Bill_Stewart Tuesday, April 14, 2020 4:36 PM This is not "fix/debug/rewrite my script for me" forum
    Tuesday, December 24, 2019 6:29 PM

All replies

  • I need help to do the following

    What kind of help do you expect?

    We are more than happy to assist with code you wrote but we do not write ready to use code on request. If you have code you wrote and have problems with it you should post this code along with error messages if you get some - both formated as code please. If you post example data you should format this as code as well please: How to Use the Code Feature in a TechNet Forum Post.

    Any suggestions would be most welcome.

    The first suggestion would be to start to learn the very basics of Powershell from scratch.

    If you like to start righ away you could watch these free video series where you learn from the inventor of Powershell: Microsoft Channel 9 - Getting Started with Microsoft PowerShell 3.0.

    Have a great Christmas Time and have a lot of fun! ;-)


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Wednesday, December 25, 2019 12:57 AM
    Wednesday, December 25, 2019 12:56 AM
  • Hi MattyMatt123

    The way you could choose is the following :

    • Read the content of file1 (Get-Content) and put the result in a var
    • Set the content of file2 with the previous var (Set-Content)

    By this way, all content of file2 is the same as File1.

    Otherwise, if you would like to add content from file1 to file2, using the Add-Content cmdlet is anoter way.

    Just some advices :

    • when you're looking for the appropriate cmdlet use the Get-Command cmdlet with joker character (*). i.e. : Get-Command *content*
    • After that, use the cmdlet Get-Help (or Help or Man as alias) to get the help about a specific cmdlet. i.e. : GetHelp Get-Content -Full return the full help about Get-Content with some samples.

    Nota : In the first previous example, i have replaced all content of file2. But if you would like to look for a specific text in file2 and replace it with another one, and only this text, it's not the good way. Take a look to the Select-String cmdlet

    Regards and merry chrismass

    Olivier

    Friday, December 27, 2019 5:17 AM
  • Thank you very much and Happy Holidays to you too.
    Monday, December 30, 2019 6:05 PM
  • Hi MattyMatt123

    The way you could choose is the following :

    • Read the content of file1 (Get-Content) and put the result in a var
    • Set the content of file2 with the previous var (Set-Content)

    By this way, all content of file2 is the same as File1.

    Otherwise, if you would like to add content from file1 to file2, using the Add-Content cmdlet is anoter way.

    Just some advices :

    • when you're looking for the appropriate cmdlet use the Get-Command cmdlet with joker character (*). i.e. : Get-Command *content*
    • After that, use the cmdlet Get-Help (or Help or Man as alias) to get the help about a specific cmdlet. i.e. : GetHelp Get-Content -Full return the full help about Get-Content with some samples.

    Nota : In the first previous example, i have replaced all content of file2. But if you would like to look for a specific text in file2 and replace it with another one, and only this text, it's not the good way. Take a look to the Select-String cmdlet

    Regards and merry chrismass

    Olivier

    Hello Gentlemen,

    So I have an example file(a1.txt) as below;

    -------

    a1.txt

    ----------

    #
    #
    #           CHICAGO                   7030
    #           CAIRO                       7032
    #           CHAMPAIGN               7051
    #           FLINT                        7023
    #
    #
    #
    #
    #
    BEGIN
    #    CHICAGO  7030
    #
    #ID,SITEID,SITENAME
    204608,348140,NORTH GREEN
    204761,348200,NORTH BLUFORD
    204816,348245,BLUFORD
    204818,348245,BLUFORD
    END
    # CAIRO  7032

    And I am trying to get the contents between BEGIN and END words into another file by doing below;

    $start = (Select-String -Path "C:\Users\Matt\Documents\a1.txt" -Pattern "BEGIN").LineNumber;$end = (Select-String -Path "C:\Users\Matt\Documents\a1.txt" -Pattern "END").LineNumber;(Get-Content "C:\Users\Matt\Documents\a1.txt")[$start..($end)] | Set-Content .\sample.txt
    #    CHICAGO  7030
    #
    #ID,SITEID,SITENAME
    204608,348140,NORTH GREEN
    204761,348200,NORTH BLUFORD
    204816,348245,BLUFORD
    204818,348245,BLUFORD
    END
    # CAIRO  7032

    The above is the sample.txt file that I am getting. I do not want END and anything below it. Any tips would be much appreciated.

    - Happy New Year 2020 !

    Matt


    Monday, December 30, 2019 7:59 PM
  • Use the -raw switch to read the entire contents of the file into a variable. 

    https://devblogs.microsoft.com/scripting/powertip-read-text-file-in-raw-mode-with-powershell/

    Use the string .indexof() method to find the location of the BEGIN and END strings. You may need to use .LastIndexOf() if the letters 'END' occur as part of the data.

    https://devblogs.microsoft.com/scripting/powershell-string-theory/

    Use the .SubString() method to extract the data. Increment the starting position to skip over the word BEGIN and calculate the length.

    https://www.itechguides.com/powershell-substring/

    Or..... if you don't use -raw, then the variable that Get-Content returns will contain an array of the lines in the file. Use a foreach ($line in $lines) and examine each line individually. When $line contains BEGIN, set a flag to indicate that any subsequent $line should be concatenated into your output variable. When $line contains END, terminate the foreach.

    https://www.computerperformance.co.uk/powershell/foreach/

     
    • Edited by MotoX80 Tuesday, December 31, 2019 12:00 AM
    Monday, December 30, 2019 11:52 PM
  • As usual you have several options to achieve your requirement:
    $FilePath = 'C:\Users\Matt\Documents\a1.txt'
    (Get-Content -Path $FilePath -Raw) -match '(?si)(?=BEGIN).*(?<=END)'
    $Matches[0] -split "`n" | Select-Object -Skip 1 | Select-Object -SkipLast 1
    
    #or
    
    (Get-Content -Path $FilePath -Raw) -replace '(?si).*BEGIN' -replace '($si)END.*'
    I'm pretty sure there are some more options  ... maybe even some more sophisticated ones. 

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Monday, December 30, 2019 11:55 PM
  • Thank you MotoX80...what you say makes sense....however, I will try what you suggested after I fail to achieve  what BOfH-666 is suggesting...
    Thursday, January 2, 2020 3:05 PM
  • ...  after I fail to achieve  what BOfH-666 is suggesting...
    I'm confused. Did you try the code snippets I suggested last?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Thursday, January 2, 2020 3:13 PM
    Thursday, January 2, 2020 3:12 PM
  • To match clear blocks of text across lines use the following.

    $file1 = @'
    #
    #
    #           CHICAGO                   7030
    #           CAIRO                       7032
    #           CHAMPAIGN               7051
    #           FLINT                        7023
    #
    #
    #
    #
    #
    BEGIN
    #    CHICAGO  7030
    #
    #ID,SITEID,SITENAME
    204608,348140,NORTH GREEN
    204761,348200,NORTH BLUFORD
    204816,348245,BLUFORD
    204818,348245,BLUFORD
    END
    # CAIRO  7032
    '@
    PS C:\scripts> $file1 -match '(?smi)BEGIN(.*)END'
    True
    PS C:\scripts> $matches[1]
    
    #    CHICAGO  7030
    #
    #ID,SITEID,SITENAME
    204608,348140,NORTH GREEN
    204761,348200,NORTH BLUFORD
    204816,348245,BLUFORD
    204818,348245,BLUFORD
    
    PS C:\scripts>
    

    To get the input from a file use the following:

    $file1 = Get-Content file1.txt -Raw


    \_(ツ)_/

    • Proposed as answer by jrv Thursday, January 2, 2020 3:34 PM
    Thursday, January 2, 2020 3:34 PM
  • I'm confused. Did you try the code snippets I suggested last? 

    I must admit that I do not have a lot of experience with regular expressions. In your second example you have '($si)'. Shouldn't that be '(?si)'?  

    I did some playing around and came up with this. It removes the new line characters. 

    (Get-Content -Path $FilePath -Raw) -replace '(?s).*BEGIN.\n'  -replace '(?s)\nEND.*'

    I referenced https://www.regular-expressions.info/refmodifiers.html

    Since Mattymatt is new to PS, maybe you could post a link to your favorite web site that explains regular expressions.  

    Thursday, January 2, 2020 4:03 PM
  • I'm confused. Did you try the code snippets I suggested last? 

    I must admit that I do not have a lot of experience with regular expressions. In your second example you have '($si)'. Shouldn't that be '(?si)'?  

    I did some playing around and came up with this. It removes the new line characters. 

    (Get-Content -Path $FilePath -Raw) -replace '(?s).*BEGIN.\n'  -replace '(?s)\nEND.*'

    I referenced https://www.regular-expressions.info/refmodifiers.html

    Since Mattymatt is new to PS, maybe you could post a link to your favorite web site that explains regular expressions.  

    I just posted the correct solution to the last example from the OP.  The OP needs to learn basic PowerShell and actually try to understand why his requests are beyond the scope of this forum.

    To test a line break use $ and not \n.  To test a line beginning use ^ and not *. 

    Use "-match" and a capture to retrieve the strings between the two block delimiter strings.  Almost any other method will run into issues.

    Note that the rules for Java or Javacript will not work in PowerShell for most control sequences.

    (?sm)BEGIN(.*)END' 

    (?sm) - the "m" causes the match  across lines which is required.  This can also be set using [regex] options.

    This will be the most portable solution and will isolate the match strings on a single line.

    $file1 -cmatch '(?sm)^BEGIN$(.*)^END$'

    Note that this enforces the uppercase match. "-cmatch" is case sensitive.

    Also note that when reading from a file the first line break character is "\r" and not "\n" in Windows.  The $ matches the full terminator in any system.  Windows use "\r\n".  Unix uses "\n".  Other systems may use different terminating sequences.


    \_(ツ)_/


    • Edited by jrv Thursday, January 2, 2020 4:46 PM
    Thursday, January 2, 2020 4:41 PM
  • Hi BOfH-66,

    What you suggested did work and to remove the newline spaces between lines I tried the following; 

    (gc .\old.txt).Trim() | ? {$_.Length -gt 0} | Set-Content .\old.txt

    However, my issue is different;

    As I initially posted - I am trying to find the location/line of ("BEGIN") string in original file and then remove/replace the data from that location till ("END") string is reached. Then copy new data from another file into the line where "BEGIN" appeared. 

    The following is the logic that I am trying to implement(in Powershell !);

    Given an original file;

    Read the contents line by line, **this file is converted to a single string by -Raw cmdlet

    if(String_found.equals("BEGIN"){

    get the line number of the "BEGIN" string ** dont know if after converting to string a line can be fetched

    start deleting line after line till "END" string is reached ** dont know if after converting to string a line can be fetched

    go back to line number of "BEGIN" and from next line copy contents from new file into original file

    }

    - MotoX80 gave me hints...but I cant do it in Powershell

    Thursday, January 2, 2020 6:14 PM
  • Why do you need to remove the line breaks. That is not what you asked for and it does not serve any purpose.

    Without the line breaks the contents will be meaningless to any human or piece of software.

    Perhaps you need to rethink what you are trying to do.


    \_(ツ)_/

    Thursday, January 2, 2020 6:19 PM
  • The following line will replace all newline characters.

     $matches[1] -replace "`n"


    \_(ツ)_/

    Thursday, January 2, 2020 6:22 PM
  • I'm confused. Did you try the code snippets I suggested last? 

    I must admit that I do not have a lot of experience with regular expressions. In your second example you have '($si)'. Shouldn't that be '(?si)'?  

    I did some playing around and came up with this. It removes the new line characters. 

    (Get-Content -Path $FilePath -Raw) -replace '(?s).*BEGIN.\n'  -replace '(?s)\nEND.*'

    I referenced https://www.regular-expressions.info/refmodifiers.html

    Since Mattymatt is new to PS, maybe you could post a link to your favorite web site that explains regular expressions.  

    I just posted the correct solution to the last example from the OP.  The OP needs to learn basic PowerShell and actually try to understand why his requests are beyond the scope of this forum.

    To test a line break use $ and not \n.  To test a line beginning use ^ and not *. 

    Use "-match" and a capture to retrieve the strings between the two block delimiter strings.  Almost any other method will run into issues.

    Note that the rules for Java or Javacript will not work in PowerShell for most control sequences.

    (?sm)BEGIN(.*)END' 

    (?sm) - the "m" causes the match  across lines which is required.  This can also be set using [regex] options.

    This will be the most portable solution and will isolate the match strings on a single line.

    $file1 -cmatch '(?sm)^BEGIN$(.*)^END$'

    Note that this enforces the uppercase match. "-cmatch" is case sensitive.

    Also note that when reading from a file the first line break character is "\r" and not "\n" in Windows.  The $ matches the full terminator in any system.  Windows use "\r\n".  Unix uses "\n".  Other systems may use different terminating sequences.


    \_(ツ)_/


    Thank you jrv and others for all the help and suggestions.

    However, my issue is different;

    As I initially posted - I am trying to find the location/line of ("BEGIN") string in original file and then remove/replace the data from that location till ("END") string is reached. Then copy new data from another file into the line after "BEGIN" appeared. 

    The following is the logic that I am trying to implement(in Powershell !);

    Given an original file;

    Read the contents line by line, **this file is converted to a single string by -Raw cmdlet

    if(String_found.equals("BEGIN"){
    get the line number of the "BEGIN" string ** dont know if after converting to string a line can be fetched
    start deleting line after line till "END" string is reached ** dont know if after converting to string a line can be fetched
    go back to line number of "BEGIN" and from next line copy contents from new file into original file
    }

    - MotoX80 gave me hints...but I cant do it in Powershell !




    Thursday, January 2, 2020 6:24 PM
  • If you are trying to make it into an array then this works.

    $matches[1] -split "`n"

    The trick is to learn PowerShell and how it uses regular expressions and string operations to accomplish your task.  First you need to have a correct definition of the task.  That tells us which methods to use and what steps have to be done independently.


    \_(ツ)_/

    Thursday, January 2, 2020 6:27 PM
  • If you are trying to make it into an array then this works.

    $matches[1] -split "`n"

    The trick is to learn PowerShell and how it uses regular expressions and string operations to accomplish your task.  First you need to have a correct definition of the task.  That tells us which methods to use and what steps have to be done independently.


    \_(ツ)_/

    Sure. I wrote the following up earlier;

    - I have an original file (let me call it File 1) that has following contents;

    ---------

    File 1.dat

    --------

    #
    #
    #           CHICAGO                   7030
    #           CAIRO                       7032
    #           CHAMPAIGN               7050
    #           FLINT                        7023
    #
    #
    #
    #
    #
    #
    #    CHICAGO  7030
    #
    #ID,SITEID,SITENAME
    204608,348140,NORTH GREEN
    204761,348200,NORTH BLUFORD
    204816,348245,BLUFORD
    204818,348245,BLUFORD
    #
    # CAIRO  7032

    #ID,SITEID,SITENAME
    201448008,343235,BUDA
    201448309,343246,BUDA
    306375,343110,FILLMORE
    306376,343110,FILLMORE
    306394,343105,FILLMORE
    #
    #
    # CHAMPAIGN                      7050
    #
    #ID,SITEID,SITENAME
    56637116,341620_2408,240_95_IS
    57178058,341275_1655,165_50_IS
    57180467,341390_1768,176_78_IS
    #
    #

    There is another file ( file 2 ) which is updated by a Web Service which basically updates data of a Division.

    --------------

    File 2.dat

    -------------

    #
    #           CHICAGO                   7030
    #           CAIRO                       7032
    #           CHAMPAIGN               7051
    #           FLINT                        7023
    #
    #
    # CHAMPAIGN                      7051
    #
    #ID,SITEID,SITENAME
    newdata,newdata,newdata
    newdata,newdata,newdata
    newdata,newdata,newdata
    #

    - I want to update/replace the existing data that is present in File 1 with the new data in File 2 - with the Division that is updated in File 2. Basically copy the contents from File 2 to File 1 at the location where Division name is (# CHAMPAIGN 7050), also update the Division version (from 7050 to 7051) at two places.

    Thursday, January 2, 2020 6:32 PM
  • I'm confused. Did you try the code snippets I suggested last? 

    I must admit that I do not have a lot of experience with regular expressions. In your second example you have '($si)'. Shouldn't that be '(?si)'?  

    I did some playing around and came up with this. It removes the new line characters. 

    (Get-Content -Path $FilePath -Raw) -replace '(?s).*BEGIN.\n'  -replace '(?s)\nEND.*'

    I referenced https://www.regular-expressions.info/refmodifiers.html

    Since Mattymatt is new to PS, maybe you could post a link to your favorite web site that explains regular expressions.  

    I just posted the correct solution to the last example from the OP.  The OP needs to learn basic PowerShell and actually try to understand why his requests are beyond the scope of this forum.

    To test a line break use $ and not \n.  To test a line beginning use ^ and not *. 

    Use "-match" and a capture to retrieve the strings between the two block delimiter strings.  Almost any other method will run into issues.

    Note that the rules for Java or Javacript will not work in PowerShell for most control sequences.

    (?sm)BEGIN(.*)END' 

    (?sm) - the "m" causes the match  across lines which is required.  This can also be set using [regex] options.

    This will be the most portable solution and will isolate the match strings on a single line.

    $file1 -cmatch '(?sm)^BEGIN$(.*)^END$'

    Note that this enforces the uppercase match. "-cmatch" is case sensitive.

    Also note that when reading from a file the first line break character is "\r" and not "\n" in Windows.  The $ matches the full terminator in any system.  Windows use "\r\n".  Unix uses "\n".  Other systems may use different terminating sequences.


    \_(ツ)_/


    Thank you jrv and others for all the help and suggestions.

    However, my issue is different;

    As I initially posted - I am trying to find the location/line of ("BEGIN") string in original file and then remove/replace the data from that location till ("END") string is reached. Then copy new data from another file into the line after "BEGIN" appeared. 

    The following is the logic that I am trying to implement(in Powershell !);

    Given an original file;

    Read the contents line by line, **this file is converted to a single string by -Raw cmdlet

    if(String_found.equals("BEGIN"){
    get the line number of the "BEGIN" string ** dont know if after converting to string a line can be fetched
    start deleting line after line till "END" string is reached ** dont know if after converting to string a line can be fetched
    go back to line number of "BEGIN" and from next line copy contents from new file into original file
    }

    - MotoX80 gave me hints...but I cant do it in Powershell !




    To do this will take some fancy programming and it can be done using RegEx or by just enumerating the lines in a complex script that tests each line of the file array and acts according to the lines found. 

    You will have to spend some time learning how to work with files in PowerShell and how to code simple matches. 

    Start small and learn how to enumerate the lines in a file.

    Get-Content file.txt |
         ForEach-Object{
                # logic to detect lines and output lines to be saved.
        } |
    Out-File newfile.txt
    The line detection logic can be driven with a simple if structure.  Detect "BEGIN" then output the new text then continue reading the lines and not outputting them until you read the "END" line.  After that line just continue outputting the remaining lines.


    \_(ツ)_/

    Thursday, January 2, 2020 6:34 PM
  • If you are trying to make it into an array then this works.

    $matches[1] -split "`n"

    The trick is to learn PowerShell and how it uses regular expressions and string operations to accomplish your task.  First you need to have a correct definition of the task.  That tells us which methods to use and what steps have to be done independently.


    \_(ツ)_/

    Sure. I wrote the following up earlier;

    - I have an original file (let me call it File 1) that has following contents;

    ---------

    File 1.dat

    --------

    #
    #
    #           CHICAGO                   7030
    #           CAIRO                       7032
    #           CHAMPAIGN               7050
    #           FLINT                        7023
    #
    #
    #
    #
    #
    #
    #    CHICAGO  7030
    #
    #ID,SITEID,SITENAME
    204608,348140,NORTH GREEN
    204761,348200,NORTH BLUFORD
    204816,348245,BLUFORD
    204818,348245,BLUFORD
    #
    # CAIRO  7032

    #ID,SITEID,SITENAME
    201448008,343235,BUDA
    201448309,343246,BUDA
    306375,343110,FILLMORE
    306376,343110,FILLMORE
    306394,343105,FILLMORE
    #
    #
    # CHAMPAIGN                      7050
    #
    #ID,SITEID,SITENAME
    56637116,341620_2408,240_95_IS
    57178058,341275_1655,165_50_IS
    57180467,341390_1768,176_78_IS
    #
    #

    There is another file ( file 2 ) which is updated by a Web Service which basically updates data of a Division.

    --------------

    File 2.dat

    -------------

    #
    #           CHICAGO                   7030
    #           CAIRO                       7032
    #           CHAMPAIGN               7051
    #           FLINT                        7023
    #
    #
    # CHAMPAIGN                      7051
    #
    #ID,SITEID,SITENAME
    newdata,newdata,newdata
    newdata,newdata,newdata
    newdata,newdata,newdata
    #

    - I want to update/replace the existing data that is present in File 1 with the new data in File 2 - with the Division that is updated in File 2. Basically copy the contents from File 2 to File 1 at the location where Division name is (# CHAMPAIGN 7050), also update the Division version (from 7050 to 7051) at two places.

    My previous post tells you exactly how to do that.  You will have to spend some time to understand how to get and match the information you need.  This can be done but it is not trivial.

    Normally I charge about $500 dollars and possibly more to create code to convert this kind of file request.  Most charge way more than I do. 

    Your request is not trivial.  This is not a free code writing forum.  You can ask questions about scripting but the design and writing of code is entirely your responsibility.

    If you do not know how to design and write code/programs then you will need to hire a consultant.


    \_(ツ)_/

    Thursday, January 2, 2020 6:39 PM
  • If you are trying to make it into an array then this works.

    $matches[1] -split "`n"

    The trick is to learn PowerShell and how it uses regular expressions and string operations to accomplish your task.  First you need to have a correct definition of the task.  That tells us which methods to use and what steps have to be done independently.


    \_(ツ)_/

    Sure. I wrote the following up earlier;

    - I have an original file (let me call it File 1) that has following contents;

    ---------

    File 1.dat

    --------

    #
    #
    #           CHICAGO                   7030
    #           CAIRO                       7032
    #           CHAMPAIGN               7050
    #           FLINT                        7023
    #
    #
    #
    #
    #
    #
    #    CHICAGO  7030
    #
    #ID,SITEID,SITENAME
    204608,348140,NORTH GREEN
    204761,348200,NORTH BLUFORD
    204816,348245,BLUFORD
    204818,348245,BLUFORD
    #
    # CAIRO  7032

    #ID,SITEID,SITENAME
    201448008,343235,BUDA
    201448309,343246,BUDA
    306375,343110,FILLMORE
    306376,343110,FILLMORE
    306394,343105,FILLMORE
    #
    #
    # CHAMPAIGN                      7050
    #
    #ID,SITEID,SITENAME
    56637116,341620_2408,240_95_IS
    57178058,341275_1655,165_50_IS
    57180467,341390_1768,176_78_IS
    #
    #

    There is another file ( file 2 ) which is updated by a Web Service which basically updates data of a Division.

    --------------

    File 2.dat

    -------------

    #
    #           CHICAGO                   7030
    #           CAIRO                       7032
    #           CHAMPAIGN               7051
    #           FLINT                        7023
    #
    #
    # CHAMPAIGN                      7051
    #
    #ID,SITEID,SITENAME
    newdata,newdata,newdata
    newdata,newdata,newdata
    newdata,newdata,newdata
    #

    - I want to update/replace the existing data that is present in File 1 with the new data in File 2 - with the Division that is updated in File 2. Basically copy the contents from File 2 to File 1 at the location where Division name is (# CHAMPAIGN 7050), also update the Division version (from 7050 to 7051) at two places.

    My previous post tells you exactly how to do that.  You will have to spend some time to understand how to get and match the information you need.  This can be done but it is not trivial.

    Normally I charge about $500 dollars and possibly more to create code to convert this kind of file request.  Most charge way more than I do. 

    Your request is not trivial.  This is not a free code writing forum.  You can ask questions about scripting but the design and writing of code is entirely your responsibility.

    If you do not know how to design and write code/programs then you will need to hire a consultant.


    \_(ツ)_/

    You are correct. This will take lots of work and time to code. And I surely dont have the money to pay.

    However, I will try the logic that you suggested. And ask questions if I am stuck somewhere.

    Thanks.

    Thursday, January 2, 2020 6:44 PM
  • You are correct. This will take lots of work and time to code. And I surely dont have the money to pay.

    However, I will try the logic that you suggested. And ask questions if I am stuck somewhere.

    Thanks.

    Yes. That is how this and other technical forums work. Get started and ask specific questions as you move forward but you will need to learnbasic PowerShell first.

    Learning to script properly with PowerShell



    \_(ツ)_/

    Thursday, January 2, 2020 6:56 PM