I have a file, fileA, that consists of two fields. Field 1 has an old object name, and field 2 has a new object name. I would like to search and replace a master file, FileB, by substituting the old object name (field 1 in fileA) with the new object name ( field 2 in fileA).
#!/bin/ksh
typeset -A newname;
while read old new; do
newname[$old]=$new
done < FileA
while read field1a field1b empl;do
echo "$field1a $field1b ${newname[$empl]}"
done < FileB > FileB.new
There could be a problem with this setup if any of those employees have spaces in their names. In that case I think you may need to use a different field separator in the files instead of a space and use "" around that variable names.
Thanks again for the input. How can I use vi command inside the shell script (/bin/sh) to make the substitutions and save the updates to a new file? I am having problems with the format using sed.
---------- Post updated 10-26-09 at 01:44 AM ---------- Previous update was 10-25-09 at 11:29 PM ----------
Oops,
These solutions would only work if every name in FileB needs to get changed. If some of the names have to remain unchanged then we need a different solution.
And alternatively with ksh it is efficient and fast too:
#!/bin/ksh
typeset -A newname;
while read old new; do
newname[$old]=$new
done < FileA
while read field1a field1b empl; do
if [[ -n ${newname[$empl]} ]]; then
empl=${newname[$empl]}
fi
echo "$field1a $field1b $empl"
done < FileB > FileB.new
But that is still only one field per line. I don't understand why that would not work or what would not work. Can you show me an anonymized sample of what it does to your input? Can you give me an anonymized sample of FileA as well?
the awk statement only updates the first line for example:
/------------------John Doe (new name)---------------------/
Employee name1: Tom Smith (old name)
Address:
City State:
Telephone Number:
Cellular Number:
Department:
Manager:
OK then both of your input files are different then originally specified and the names contain spaces. I wonder what you FileA looks like, since you can't use a space as a separator because the number of fields will vary (e.g. my real name is S. C. Rut in Izer, which is a very common name where I live). Instead you could use a colon or some other field separator, like so:
old_name1:new_name1
old_name3:new_name3
Tom Smith:John Doe
You can then use the separator in the script to read the old and the new name. The following should work:
cp -p FileB FileB.new
while IFS=: read -r old new; do
sed -i "s/$old/$new/" FileB.new
done < FileA
Is this a homework?
Because good programmer will not take this approach.
Usually you come up with an employee ID.
This will be a disaster if there were more than one employee with the same name.
Master file1:
Jack Jim
Tom Tim
Red Rose
Steve Stan
Jack John
Data file2:
Jack
Tom
Jack
Red
Steve
Jack
All the Jack's will be replaced with John (being the last entry in file1).
Jim will be nowhere in the picture.