Visit Our UNIX and Linux User Community

Problem updated file with new entries

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem updated file with new entries
# 1  
Old 10-26-2009
Problem updated file with new entries


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).

old_name1 new_name1
old_name2 new_name2
old_name3 new_name3

Employee name: old_name1
Employee name: old_name2
Employee name: old_name3

#! /bin/sh -vx
for i in FileA
OLD=`echo $i | awk '{print $1}'`
NEW=`echo $i | awk '{print $2}'`

sed "s/$OLD/$NEW/g" FileB >>

The above code does not evaluate the $NEW variable. Can someone please assist?


Last edited by Franklin52; 10-26-2009 at 05:39 AM.. Reason: Please use code tags!
# 2  
Old 10-26-2009
Looks like there is a problem in the for loop; why don't you change the for loop to the following:

while read i
 old=`echo $i | awk '{print $1}'`
 new=`echo $i | awk '{print $2}'`

done < FileA

Last edited by Franklin52; 10-26-2009 at 05:39 AM.. Reason: Please use code tags!
# 3  
Old 10-26-2009
Thanks for your assistance. After making the changes that you suggested, I am getting the correct substitution.

Best regards,
# 4  
Old 10-26-2009
Hi, instead of:
for i in fileA; do

for this to work you would need:
for i in $(cat fileA); do

or better, use a while loop and then you do not need the awks:

while read old new; do
  sed -n "s/$old$/$new/p" FileB
done < FileA >


If your files are not too massive you could use a faster solution:
awk 'NR==FNR {A[$1]=$2;next} {$3=A[$3]} 1' FileA FileB >

or use ksh which also has associative arrays:
typeset -A newname;
while read old new; do
done < FileA

while read field1a field1b empl;do
  echo "$field1a $field1b ${newname[$empl]}"
done < FileB >

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.

Last edited by Scrutinizer; 10-26-2009 at 04:52 AM..
# 5  
Old 10-26-2009
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.

# 6  
Old 10-26-2009
Did you try the sed I suggested?
sed -n "s/$old$/$new/p"

Otherwise you'll end up with a very long file.

---------- Post updated 10-26-09 at 01:44 AM ---------- Previous update was 10-25-09 at 11:29 PM ----------

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.
# 7  
Old 10-26-2009
Actually, I need to keep the contents of the original file in tact changing only the sed substitutions.


Previous Thread | Next Thread
Test Your Knowledge in Computers #819
Difficulty: Medium
In CSS, E ~ F matches an F element preceded by an E element.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy the last part since the file has been updated

Input File1 constatntly running and growing in size. My Program Erorr ddmmyy hh:mm:ss My Program Error **Port 123 terminated **ID PIN 12345 Comamnd Successful Command Terminated Command Successful Command Terminated **My Program Erorr ddmmyy hh:mm:ss My Program Error **Port 345... (3 Replies)
Discussion started by: eurouno
3 Replies

2. Shell Programming and Scripting

Is there any command to know what updated last in file?

Is there any command to know what updated last in file? (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

3. Shell Programming and Scripting

Search for a recently updated file

Hi, I am looking for a command to search for a specific file which was recently modified in the current directory leaving some unwanted files to be listed. For example, when I try ls - lrt it shows the following output. I want to ommit the files with the name 'resend' and... (3 Replies)
Discussion started by: svajhala
3 Replies

4. Shell Programming and Scripting

trying to check if the file is getting updated or not

#!/bin/sh #set -x Current_Date=`date +"%b %e"` Filepmdate=`ls -ltr /file/ | tail -5 | awk '{print $6,$7}'` if ; then echo " " exit 0 else echo "Log files are not updated please check" exit 0 fi done > sh -x + + date +%b %e Current_Date=Aug... (2 Replies)
Discussion started by: arch12
2 Replies

5. Shell Programming and Scripting

Script to wait until file is updated

Hello, I need to evaluate (under BASH) if the certain file has been updated or not. If the file still wasn't updated, script should wait. The script picks up the time stamp of the file using command OldTimestamp=$(date -r $MyDir/$MyFile), but I don't know how to code a waiting loop with new and... (6 Replies)
Discussion started by: sameucho
6 Replies

6. Shell Programming and Scripting

If File has been updated, do something??

Put this together from somewhere else on the forums, just modified it and added the loop. #!/bin/ksh localFile=$1 remoteFile=$2 #source FTP parameters . .ftp_put.cfg mylog=ftp_session.log echo "$(date "+%H:%M:%S") - Attempt to FTP $1 to $2" > $mylog machine="server1 server2... (5 Replies)
Discussion started by: cbo0485
5 Replies

7. Programming

Log file not getting updated

hi all, i'm a student and managing lab at my insti. there in one application in which log file has to be maintaine the number of bytes transferred and received. but after certain entries these two attributes stop getting updated and holds same values for rest of the session. This happens one time... (4 Replies)
Discussion started by: KornFire
4 Replies

8. UNIX for Dummies Questions & Answers

Find last updated file

Hi all, my problem is teh following: I need to move a file from a folder to another and I usually do it by the get command but in this case I have a list of file in the source folder and I have to select only the lust updated one. Ho to do this? All the files have the same name followed... (4 Replies)
Discussion started by: callimaco0082
4 Replies

9. Solaris

which file is updated after modified the crontab entries.

Hi all, i want to know which file is updated after changes the modifications/new entries in crontab. Please help regarding this. regards Krishna (1 Reply)
Discussion started by: krishna176
1 Replies

10. Shell Programming and Scripting

Creating an updated file

Hi, I would like to define a script in order to update a file with the last updated records: I wrote : #!/bin/sh YEAR=$(date +%Y) MONTH=$(date +%m) DAY=$(date +%d) COMM=/usr/bin/comm for file in * ; do if ] ; then FILE=${DPSTY} UNIQUE=Unique_${FILE} ... (4 Replies)
Discussion started by: dbfree
4 Replies

Featured Tech Videos