The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sed Help in Updating something only in one particular file. bisla.yogender Shell Programming and Scripting 4 03-24-2008 11:48 AM
TO know whether file is updating or not raj333 Shell Programming and Scripting 2 11-05-2007 05:29 PM
Why my sar is not updating the output file. skneeli UNIX for Advanced & Expert Users 1 11-12-2006 08:29 PM
Updating a File in a Zip Archive dbridle AIX 6 09-27-2006 03:29 PM
sending and updating a file over network bhakti High Level Programming 1 09-21-2005 02:10 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-19-2007
braindrain braindrain is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 67
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 02:23 PM..
  #2 (permalink)  
Old 06-19-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
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 (permalink)  
Old 06-19-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
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 (permalink)  
Old 06-20-2007
braindrain braindrain is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 67
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 04:16 AM..
  #5 (permalink)  
Old 06-20-2007
braindrain braindrain is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 67
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 03:24 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0