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...
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
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
Hi,
regarding the reply, the script has been modified to cope with new requirement of checking last servel characters in a column for replacement.
config file:
001,333
port.csv:
1,2,3,4,5,6,7,8,A-B-001,NNN
expected result:
1,2,3,4,5,6,7,8,A-B-333,NNN
new script:
#!/usr/bin/ksh #input file for process
FILENAME=port.csv #config file for replacement
CONFIG_TXT=cfg.txt #output file for result
OUTPUT=port_result.csv
==================
thw above script works wel in nawk. However, I later found out that there is no nawk command in a server and awk must be used. No matter how I modified the script using awk.. it won't work..can someone help me? FNR not supported in awk?
nawk 'BEGIN{FS=","}
{
if(NR==FNR)
a[$1]=$2
else
{
if(index($0,"document")!=0)
{ for (i in a)
{
if(index($0,i)!=0)
gsub(i,a,$0)
}
print
}
else
{
print
}
}
}' b a