![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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! |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
what have you tried so far ? Please show us that. We could help you in completing that
|
|
#4
|
||||
|
||||
|
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
|
|
#5
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|
#7
|
|||
|
|||
|
thx alot for yr explanation
|
|||
| Google The UNIX and Linux Forums |