The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 08:09 AM
Extracting data from text file based on configuration set in config file suparnbector Shell Programming and Scripting 3 08-10-2007 02:25 AM
Read words from file and create new file using K-shell. bsrajirs Shell Programming and Scripting 4 06-01-2007 12:15 PM
how to replace a text inside a file based on a xml key reldb Shell Programming and Scripting 4 03-11-2007 04:15 AM
How to replace a word with a series of words in a file brap45 Shell Programming and Scripting 2 02-21-2006 02:33 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-22-2008
kinmak kinmak is offline
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!
  #2 (permalink)  
Old 04-22-2008
quine quine is offline
Registered User
  
 

Join Date: Mar 2008
Location: Bay Area California
Posts: 68
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....
  #3 (permalink)  
Old 04-22-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
what have you tried so far ? Please show us that. We could help you in completing that
  #4 (permalink)  
Old 04-22-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
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.
  #5 (permalink)  
Old 04-23-2008
kinmak kinmak is offline
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
  #6 (permalink)  
Old 04-23-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
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.
  #7 (permalink)  
Old 04-24-2008
kinmak kinmak is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 17
thx alot for yr explanation
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0