Updating file in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Updating file in loop
# 1  
Old 06-19-2007
Updating file in loop

Hi,
I have a file list containing list of characters to be searched and replaced 'file_master'. I have a datafile 'file_data'.
Now, in a loop how do I ensure I'm updating the latest updated file_data when I read the next line of file_master ?


file_master
a , 1
b , 2

file_ data
hello abc
baby
barbe
.....

Quote:
while read dat_file;do
for lkp_file in `cat file_master`
do
echo $dat_file | sed -e "s,fieldtobereplaced,fieldreplacedvalue,g"
done < $2
done < $1
where $1 is file_data and and $2 is file_master.



i.e I expect output to be

file_ data :

hello 12c
212y
21r2e


Does the above, make sure for each and every line of master, data is updated ? . It doesn't seem to work

Last edited by braindrain; 06-19-2007 at 03:23 PM..
# 2  
Old 06-19-2007
Code:
while read mLine
do
  set -- `echo $mLine | sed 's/,/ /'`
  sed "s/$1/$2/" file_data > $$Temp_File
  mv $$Temp_File file_data
done < file_master
rm -f $$Temp_File

# 3  
Old 06-19-2007
Another way is to use the tr command.
Code:
while read line_master
do
   set -- `echo $line_master | sed 's/,/ /'`
   from="$from$1"
   to="$to$2"
done < file_master
tr "$from" "$to" < file_data >file_data.$$
mv file_data.$$ file_data

The problem with this solution is that special characters, like space, comma, double quote, backslash arent't supported (same thing for Shell_life solution)
# 4  
Old 06-20-2007
aigles/shell_life, thanks for the reponse.

fyi, I'm trying to replace all the special characters like
Ø Ù Ú Û Ü Ý Þ ß etc.,

For those charcter you mentioned(spaces, backslash, single quote, comma),Cant we use a escape sequence? For instance I can used sed to change comma, if I use different delimiter other than comma.

Last edited by braindrain; 06-20-2007 at 05:16 AM..
# 5  
Old 06-20-2007
To be precise,
my file_master can have following character, field1 need to be converted to field2 ( \,+\-,\\ etc)

Quote:
§ \
± +\-
¶ \\
¼ 1\4
½ 1\2
¾ 3\4
÷ \
Ofcourse I can do with multiple sed's
like below

sed -e 's,÷,\\,g' $src > $src$$
[[ $? -ne 0 ]] && [[ -s $src ]] && mv $src$$ $src || exit 3
sed -e 's,±,\\,g' $src > $src$$
[[ $? -ne 0 ]] && [[ -s $src ]] && mv $src$$ $src || exit 4


but wandering if it can be achevied in master and data file approach (without hardcoding in sed command) .

I tried, giving input as ,÷,\\ and ,±,\\ , however when it reads it strips one of the backslash and sed fails.

Last edited by braindrain; 06-20-2007 at 04:24 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Updating in file

hi, i have an csv(which is a month's log file containing userid and log in date ) file which has to be appended to another file say master.log.I need to compare the next month's log data to master.log file .In case, there is new log date for userid it has to get updated in master.log file or i... (2 Replies)
Discussion started by: preema
2 Replies

2. UNIX for Dummies Questions & Answers

Updating a File

Hi, I have a below requirement and need your help in this regard. I have two timestamps in a file i.e. TimeBefore=2014-07-10.18.06.55 TimeAfter_1=2014-07-09.05.45.12 TimeAfter_2=2014-07-09.05.40.12 When I first run the script file, it sets the 'TimeBefore' to todays date. When I run... (5 Replies)
Discussion started by: vrupatel
5 Replies

3. Shell Programming and Scripting

Help updating a file

I can not seem to figure out how to update the attached match.txt column 2 using update.txt. However, only the text before the period in updat.txt appears in match.txt. For example, in update.txt NM_001613.2 matches NM_001613 in match.txt, so is it possible to update the record in match.txt to... (8 Replies)
Discussion started by: cmccabe
8 Replies

4. Shell Programming and Scripting

updating a file with sed -help

I have a file of records all made up of single charactors that need to be updated using their row/column (the dashes represent spaces as the html formatting of this text box when it posts removes the "nonessential" white space between the charactors) file example A 1 c B 2 b C 3 a... (2 Replies)
Discussion started by: dboward
2 Replies

5. Shell Programming and Scripting

Reading from one file and updating other file with constant entries

Hi All, Thaks for the help in my last thread. I have one more question. I have two files with ldap entries in it. One file contains all the user LDAP parameter entries (26 MB) and other file contains only the user id's that have to be inactivated. Unix script has to read from the file that has... (8 Replies)
Discussion started by: Samingla
8 Replies

6. UNIX for Dummies Questions & Answers

Updating a field in a File without creating temp file's

Hi Experts, I have a requirement where i need to update the below items in file, 1. END TIME 2. PREV_STATUS For the first time the PREV_status and end time of all job the job will be sysdate & NULL reply as below, Session_name,Load Type,Frequency,Seesion End time,Prev_Status... (2 Replies)
Discussion started by: prabhutkl
2 Replies

7. Shell Programming and Scripting

Updating the input file

Hi Experts, I have a requirement. input_file.txt Emp_no Emp_Name Salary 100 A 1000 101 B 2500 102 C 1500 I want to update the salary field of a record based on a condition(like emp_no=100). Presently I am using a temperory file for updation. Eg: read the input_file if $1=100... (1 Reply)
Discussion started by: prabhutkl
1 Replies

8. Shell Programming and Scripting

awk updating one file with another, comparing, updating

Hello, I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea... (11 Replies)
Discussion started by: mecano
11 Replies

9. Shell Programming and Scripting

Sed Help in Updating something only in one particular file.

Hi, The scenerio is that I want to replace a text in one particular line of a file. But when I am using the sed it's replacing all the occurences of that text. Like the file is: >cat test DNGGF10 :None :Test DNGGF11 :ABC :Test1 DNGGF12 :None :Test2 DNGGF13 :None :... (4 Replies)
Discussion started by: bisla.yogender
4 Replies

10. Shell Programming and Scripting

TO know whether file is updating or not

Hi All, I am new to scripting. my requirement is , I want to know whether the file(i.e., log file) is updating or not. It should search for every 15 min. If it is not updating means it needs to send out a automatic mail. can you please help me in this. (2 Replies)
Discussion started by: raj333
2 Replies
Login or Register to Ask a Question