Matching and Replacing file lines starting with $


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching and Replacing file lines starting with $
# 1  
Old 08-13-2014
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 $

Code:
 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
# 2  
Old 08-13-2014
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?
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-13-2014
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":
Code:
 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"
Code:
 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:

Code:
 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
# 4  
Old 08-13-2014
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.

Last edited by Corona688; 08-13-2014 at 04:50 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 08-13-2014
Hi, try:
Code:
        .....
        $0 in a {
                    $0=a[$0]
                    }1
        '  deckToChange >newDeck

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 08-14-2014
Closing thread since you have made a new one on this topic.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep file starting from pattern matching line

I have a file with a list of references towards the end and want to apply a grep for some string. text .... @unnumbered References @sp 1 @paragraphindent 0 2017. @strong{Chalenski, D.A.}; Wang, K.; Tatanova, Maria; Lopez, Jorge L.; Hatchell, P.; Dutta, P.; @strong{Small airgun... (1 Reply)
Discussion started by: kristinu
1 Replies

2. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

3. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

4. Shell Programming and Scripting

Select only the lines of a file starting with a field which is matcing a list. awk?

Hello I have a large file1 which has many events like "2014010420" and following lines under each event that start with text . It has this form: 2014010420 num --- --- num .... NTE num num --- num... EFA num num --- num ... LASW num num --- num... (9 Replies)
Discussion started by: phaethon
9 Replies

5. Shell Programming and Scripting

awk Array matching and replacing from master file.

I have an awk related question that I was hoping you all could help with. I am given 2 input files named OLDFILE and NEWFILE, and a Master file named MASTERFILE. They can be seen below. OLDFILE: a a a a a f g 4 5 7 8 1 2 3 (1 Reply)
Discussion started by: tiktak292
1 Replies

6. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

7. Shell Programming and Scripting

Read .txt file and dropping lines starting with #

Hi All, I have a .txt file with some contents as below: Hi How are you? # Fine and you? I want a script file which reads the .txt file and output the lines which does not start with #. Hi How are you? Help is highly appreciated. Please use code tags when posting data and... (5 Replies)
Discussion started by: bghosh
5 Replies

8. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

9. Shell Programming and Scripting

awk if statement matching all lines starting w/ a number

Does awk have a syntax for a range of numbers or is this the best way? if ($1 >= 0 && $1 <= 9 ) (7 Replies)
Discussion started by: Arsenalman
7 Replies

10. Shell Programming and Scripting

How to print the number of lines from a file, the starting string should be passed`

Hi , I have file, which has the below content: line 100 a b c d line300 a s d f s line200 a s d a (3 Replies)
Discussion started by: little_wonder
3 Replies
Login or Register to Ask a Question