Unix - Need to Find & Replace based on a specific string

Unix - Need to Find & Replace based on a specific string

Post by ambroz » Sun, 25 Jun 2006 00:01:37



Here's what I'm trying to do:

I have file#1 - Here is what's in it:

"00024","111111111A","111111111","DOE","JOHN","RECORD ERROR BLAH"
"00024","222222222A","222222222","DOE","JANE","RECORD ERROR BLAH"
"00024","333333333A","333333333","DOE","DICK","RECORD ERROR BLAH"
"00024","444444444A","444444444","DOE","RON","RECORD ERROR BLAH"

I have file#2 - Here is what's in it:

"00024","111111111A","111111111","DOE","JOHN","COMM ERROR"
"00024","222222222A","222222222","DOE","JANE","COMM ERROR"
"00024","333333333A","333333333","DOE","DICK","COMM ERROR"
"00024","444444444A","444444444","DOE","RON","COMM ERROR"

I need to match up these the records in file#1 using the 2nd, 4th and
5th delimited information with file#2 and replace the entire line from
file#1 to file#2. The trick is the records in file#2 are going to
differ from the records in file#1. They could be anywhere throughout
the file.

TIA

 
 
 

Unix - Need to Find & Replace based on a specific string

Post by Xicheng Ji » Sun, 25 Jun 2006 00:24:45



> Here's what I'm trying to do:

> I have file#1 - Here is what's in it:

> "00024","111111111A","111111111","DOE","JOHN","RECORD ERROR BLAH"
> "00024","222222222A","222222222","DOE","JANE","RECORD ERROR BLAH"
> "00024","333333333A","333333333","DOE","DICK","RECORD ERROR BLAH"
> "00024","444444444A","444444444","DOE","RON","RECORD ERROR BLAH"

> I have file#2 - Here is what's in it:

> "00024","111111111A","111111111","DOE","JOHN","COMM ERROR"
> "00024","222222222A","222222222","DOE","JANE","COMM ERROR"
> "00024","333333333A","333333333","DOE","DICK","COMM ERROR"
> "00024","444444444A","444444444","DOE","RON","COMM ERROR"

> I need to match up these the records in file#1 using the 2nd, 4th and
> 5th delimited information with file#2 and replace the entire line from
> file#1 to file#2. The trick is the records in file#2 are going to
> differ from the records in file#1. They could be anywhere throughout
> the file.

looks like another Faq. :-)

awk -F'","'
       '{ key = $2$4$5 }
        FNR == NR { seen[key] = $0; next }
        key in seen{ $0 = seen[key] }
       ' file1 file2

perl -F'","' -alne


        print $seen{$key}||$_;
       ' file1 file2

Xicheng

 
 
 

Unix - Need to Find & Replace based on a specific string

Post by Xicheng Ji » Sun, 25 Jun 2006 00:34:36




> > Here's what I'm trying to do:

> > I have file#1 - Here is what's in it:

> > "00024","111111111A","111111111","DOE","JOHN","RECORD ERROR BLAH"
> > "00024","222222222A","222222222","DOE","JANE","RECORD ERROR BLAH"
> > "00024","333333333A","333333333","DOE","DICK","RECORD ERROR BLAH"
> > "00024","444444444A","444444444","DOE","RON","RECORD ERROR BLAH"

> > I have file#2 - Here is what's in it:

> > "00024","111111111A","111111111","DOE","JOHN","COMM ERROR"
> > "00024","222222222A","222222222","DOE","JANE","COMM ERROR"
> > "00024","333333333A","333333333","DOE","DICK","COMM ERROR"
> > "00024","444444444A","444444444","DOE","RON","COMM ERROR"

> > I need to match up these the records in file#1 using the 2nd, 4th and
> > 5th delimited information with file#2 and replace the entire line from
> > file#1 to file#2. The trick is the records in file#2 are going to
> > differ from the records in file#1. They could be anywhere throughout
> > the file.

> looks like another Faq. :-)

 awk -F'","'  '
        { key = $2$4$5 }
         FNR == NR { seen[key] = $0; next }
         key in seen{ $0 = seen[key] } 1
        ' file1 file2

sorry for a typo, forgot a '1'....

Xicheng

perl -F'","' -alne   '


         print $seen{$key}||$_;
        ' file1 file2

 
 
 

Unix - Need to Find & Replace based on a specific string

Post by ambroz » Sun, 25 Jun 2006 01:11:39


I have to say I am very impressed. This is going to be such a big help
in what I'm doing. This will save me a ton of time. Thanks a ton!!!
You ROCK!



> > > Here's what I'm trying to do:

> > > I have file#1 - Here is what's in it:

> > > "00024","111111111A","111111111","DOE","JOHN","RECORD ERROR BLAH"
> > > "00024","222222222A","222222222","DOE","JANE","RECORD ERROR BLAH"
> > > "00024","333333333A","333333333","DOE","DICK","RECORD ERROR BLAH"
> > > "00024","444444444A","444444444","DOE","RON","RECORD ERROR BLAH"

> > > I have file#2 - Here is what's in it:

> > > "00024","111111111A","111111111","DOE","JOHN","COMM ERROR"
> > > "00024","222222222A","222222222","DOE","JANE","COMM ERROR"
> > > "00024","333333333A","333333333","DOE","DICK","COMM ERROR"
> > > "00024","444444444A","444444444","DOE","RON","COMM ERROR"

> > > I need to match up these the records in file#1 using the 2nd, 4th and
> > > 5th delimited information with file#2 and replace the entire line from
> > > file#1 to file#2. The trick is the records in file#2 are going to
> > > differ from the records in file#1. They could be anywhere throughout
> > > the file.

> > looks like another Faq. :-)

>  awk -F'","'  '
>         { key = $2$4$5 }
>          FNR == NR { seen[key] = $0; next }
>          key in seen{ $0 = seen[key] } 1
>         ' file1 file2

> sorry for a typo, forgot a '1'....

> Xicheng

> perl -F'","' -alne   '


>          print $seen{$key}||$_;
>         ' file1 file2

 
 
 

1. General unix question: find & replace string in a set of files

I have a lot of files that need to be updated in a very similar manner.

Is there a simple (or cryptic) unix command to go through every file
in a directory, find every occurrence of "12 January" and replace it
with "26 February"?  All of the files are plain old text files.

I'm just dreading the thought of opening every one of them in emacs
and doing a find and replace....

Thanks in advance!

--
Phil Geiger

2. re-activate Linux partition? (RH 5.2)

3. Find a string in a text file and replace with another string to eof

4. what it network is down

5. script to recursively find files containing string and replace string

6. Looking for Colorado Memory Systems driver

7. **** FINDING & REPLACING A STRING WITHIN MANY FILES *****

8. IDT 77252 device driver...

9. Help with Script to replace string on specific rownumber.

10. Search and replace text in a file based on a specific line

11. Urgent UNIX help, find & replace

12. How to find files that contain specific string in all subdirectory