If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Help with replacing string in a file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-23-08, 03:41
dusoo dusoo is offline
Registered User
 
Join Date: May 2008
Posts: 6
Help with replacing string in a file

Hi everyone.
i would like to read file - line by line,
search for a string (with regular exp)
if found, replace it with something retrieved by a function.
Thanks for any ideas...
Reply With Quote
  #2 (permalink)  
Old 05-23-08, 14:44
Pat Phelan Pat Phelan is online now
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,612
Perl, AWK, PHP ? The part about the function lets SED off the hook, but there are still many contenders!

-PatP
Reply With Quote
  #3 (permalink)  
Old 05-26-08, 10:39
dusoo dusoo is offline
Registered User
 
Join Date: May 2008
Posts: 6
Hi,... You were not really helpfull as i expected...
Could u write an example how to replace one string in a file if matches a pattern?
thanks
(shell)
Reply With Quote
  #4 (permalink)  
Old 05-27-08, 00:08
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Have a look at Pat's suggestions and try to understand the basics of the different tools and programming languages. It is really straight-forward with Perl, for example - with the others as well, - and you can find tons of examples if you put your favorite search engine to work. Why don't give it a try?
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 05-27-08, 02:36
dusoo dusoo is offline
Registered User
 
Join Date: May 2008
Posts: 6
Hi, of course i have tried pat's suggestions...

below is the code i came up with. It's working, but it takes a lot (an hour..) to process one single (50megs "large") file. I have later even larger files to be processed...
Is there a way to faster it a bit?
Regarding the input file. There is maybe each fifth line containing the "mailD,id" string, that i will replace by other value.

thanks for any help.

Code:
#!/bin/bash

FILE=$1
while read line
do
  if [ `echo "$line" | grep "mailD,id" | wc -l` -eq 1 ]
  then
		tmpmail=`echo $line | perl -p -e 's/^.*mailD,id = ([A-Z0-9][A-Z0-9]( [A-Z0-9][A-Z0-9])+),.*$/\1/'`
  	lda=`ldapsearch -a always -D cn=$user -w $pass -h 10.16.41.10 -p 16611 -s sub -L -b "id=$tmpmail,domainName=subsD,O=Test,C=MM" objectclass=common 2> /dev/null | grep mailo | perl -p -e 's/mailo: ([0-9]+)/\1/'`
   	perl -pi -e "s/$tmpmail/$lda/" log.log
  fi 	
done < $FILE
Reply With Quote
  #6 (permalink)  
Old 05-29-08, 10:36
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
You could help us a little bit by putting a few comments in your code that explains what each part does. It takes a lot of time to try and decipher other peoples code and remember we're not being paid to do it. These are my guess's on how to make your code faster.

You're currently processing each line of your large file but really you just want to process those lines containing "mailD,id". then to get a big improvement in speed you want to try and do things via pipes rather than via loops. Why not use sed to extract this info outside the loop and just do it once for each user ie

Code:
#!/bin/bash

FILE=$1

grep "mailD,id" $FILE | \
    sed 's/^.*mailD,id = ([A-Z0-9][A-Z0-9]( [A-Z0-9][A-Z0-9])+),.*$/\1/'` | \
    sort | \
    uniq | \
    while read tmpmail
    do
  	lda=`ldapsearch -a always -D cn=$user -w $pass -h 10.16.41.10 -p 16611 -s sub -L -b "id=$tmpmail,domainName=subsD,O=Test,C=MM" objectclass=common 2> /dev/null | grep mailo | perl -p -e 's/mailo: ([0-9]+)/\1/'`
   	perl -pi -e "s/$tmpmail/$lda/" log.log
    done

exit
Before posting back that my code doesn't work etc please remember it's difficult to read your original code, there is no test data and no explanation of what's supposed to happen. You may need to use \( instead of ( in the sed command. This should be faster.

Mike
Reply With Quote
  #7 (permalink)  
Old 05-29-08, 10:51
dusoo dusoo is offline
Registered User
 
Join Date: May 2008
Posts: 6
Im sorry not leaving any comments, my mistake
I'll try your suggestion tomorrow and come back with results.
Thanks for your time
Reply With Quote
  #8 (permalink)  
Old 05-30-08, 02:36
dusoo dusoo is offline
Registered User
 
Join Date: May 2008
Posts: 6
Hi mike_bike_kite,

your script is working perfect, and it's very fast, since it's not reading file line by line

thanks a lot !
Reply With Quote
  #9 (permalink)  
Old 05-30-08, 19:54
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
no problem
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On