The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Delete words in File 1 from File 2 Enobarbus37 Shell Programming and Scripting 14 02-15-2008 05:09 AM
Extracting data from text file based on configuration set in config file suparnbector Shell Programming and Scripting 3 08-09-2007 11:25 PM
Read words from file and create new file using K-shell. bsrajirs Shell Programming and Scripting 4 06-01-2007 09:15 AM
how to replace a text inside a file based on a xml key reldb Shell Programming and Scripting 4 03-11-2007 01:15 AM
How to replace a word with a series of words in a file brap45 Shell Programming and Scripting 2 02-20-2006 11:33 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 04-22-2008
Registered User
 

Join Date: Feb 2005
Posts: 17
replace words in file based on another file

Hello,
Can someone kindy help me solve this problem..I am using SunOS shell script
I got a file A with following content:

This is my correct document. I wrote 111
This is my incorrect word , 222
This is my wrong statement 333
This is my correct document 444
This is my correct document 555

if the fifth word is document, then replace the word with a new word according to the config file.

If the content of config file B is
111,one
555,five


then the result updated in file A shoud be:

This is my correct document. I wrote one
This is my incorrect word , 222
This is my wrong statement 333
This is my correct document 444
This is my correct document five


even document is the fifth element, 444 will not be updated because 444 is not appeared in config file...

Can someone show me how to accomplish this? thx!
Reply With Quote
Forum Sponsor
  #2  
Old 04-22-2008
Registered User
 

Join Date: Mar 2008
Location: Bay Area California
Posts: 63
In what language/utility? I can do in PERL. Takes only a few lines, but to get it right (and elegant) I might have to tinker for a while....
Reply With Quote
  #3  
Old 04-22-2008
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,609
what have you tried so far ? Please show us that. We could help you in completing that
Reply With Quote
  #4  
Old 04-22-2008
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
Try:
Code:
awk '
NR==FNR {
   split($0, cfg, /,/);
   config[cfg[1]] = cfg[2];
   next;
}
$5~/^document/ && ($NF in config) {
  $NF = config[$NF];
}
1
' B A
Jean-Pierre.
Reply With Quote
  #5  
Old 04-23-2008
Registered User
 

Join Date: Feb 2005
Posts: 17
Hi aigles,
would u please kindly explain the program.. For usage of awk, I ony know how to use it to parse a string. So, I dun quite get the meaning of each ine of code.. The script should be purely unix script, not per script
Reply With Quote
  #6  
Old 04-23-2008
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
Code:
awk '

NR==FNR {                           # Select records from first file
   split($0, cfg, /,/);             #    Split record with "," into record cfg
   config[cfg[1]] = cfg[2];         #    Memorize config value
   next;                            #    Proceed next record
}                                   #

# The following code is for second file
                                     
$5~/^document/ && ($NF in config) { # Select records starting with "document" and
                                    #  last fields memorized in config record (build
									#  from first file)
  $NF = config[$NF];                #    Set last field with corresponding value from config array
}                                   #
 
1                                   # Print record (maybe modified above)

' B A
Jean-Pierre.
Reply With Quote
  #7  
Old 04-24-2008
Registered User
 

Join Date: Feb 2005
Posts: 17
thx alot for yr explanation
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0