Matching and Replacing file lines starting with $

Here is the task that I was presented with:
I am dealing with about a 10,000 line input deck file for an analysis. About 10 separate blocks of around 25 lines of code each need to be updated in the input deck.

The input deck (deckToChange in the code below) comes with 2 separate files. File 1 is just the blocks of code that need to be changed written consecutively (OLDFILE in the code below), and file 2 is the same blocks of code that have already been altered (NEWFILE in the code below)

The same process is going to be repeated with 1000's of input decks making the same changes, so the code needs to repeatable.

Right now, in the code I have written below, all the changes are properly made except for any line that starts with a $. the $ start to lines in the input deck occurs about 3 times per file. With the code below the lines starting with the $ are not updated with changes in the new file. I am wondering if there is anyway I can read in the lines of code from OLDFILE and NEWFILE with the $ being treated as $

 awk '
        BEGIN {
                   while ((getline name < "OLDFILE") > 0)
                            {
                             getline name2 < "NEWFILE"
                             a[name]=name2
                             }
                   }
        a[$0]   {
                    $0=a[$0]
                    }1
        '  deckToChange >newDeck
 

This is a rather urgent problem, and any help would be greatly appreciated.

Thanks

It's difficult to extrapolate what you do want from a program which doesn't do what you want.

Show some minimal input we can test with please, the output you presently get from it, and the output you'd rather it be doing. Be sure to include a line that demonstrates the problem.

I don't understand yet why it would ignore lines beginning with $... That's a regex special character, but regexes don't appear to be involved here. Did you remove any of your code or is this complete?

1 Like

Unfortunately I am unable to post any of my actual sample code, but I can give you an example for with similar code

a small portion of "OLDFILE":

 0   5632.3   3421   4343   10
 $END COMMAND SECTION 7.3
 5   2343.4   3434   3432   10
 3   84   34   12   34   1  
 

a small portion of "NEWFILE" at the same location as "OLDFILE"

 0   5632.3   3421   4343   15
 $4 END COMMAND SECTION 7.4
 5   2343.4   5432   3432   10
 3   84   37   27   58   1  
 

The way I have my code written, the "deckToChange" file will print to "newDeck", as is, unless the line in "deckToChange" matches a line from "OLDFILE".

The output from just the code above is:

 0   5632.3   3421   4343   15
 $4 END COMMAND SECTION 7.3
 5   2343.4   5432   3432   10
 3   84   37   27   58   1  
 

The second line does not change. I assuming this is because it starts with a $

---------- Post updated at 03:40 PM ---------- Previous update was at 03:37 PM ----------

This is also the complete code

Hm... If all you want to do is duplicate the contents of OLDFILE, why not just cp OLDFILE ... ? Your example is too short to show the entire face of the problem, I think.

[edit] I see the problem. You didn't post any portion of DeckToChange, leaving it a complete mystery, and giving us absolutely nothing to test with.

Also, this line-matching approach in general troubles me. How sure are you that there's never going to be any identical lines, at all ,ever? If that ever happens, you could replace many lines you didn't intend to.

1 Like

Hi, try:

        .....
        $0 in a {
                    $0=a[$0]
                    }1
        '  deckToChange >newDeck
1 Like

Closing thread since you have made a new one on this topic.

1 Like