The UNIX and Linux Forums  

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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help regarding replacing a part of string veerapureddy Shell Programming and Scripting 1 03-17-2008 06:02 AM
how to get the last part of a string followed by a pattern bluemoon1 Shell Programming and Scripting 4 09-08-2007 08:10 AM
Extracting part of a string sam_78_nyc Shell Programming and Scripting 8 04-25-2007 04:37 PM
ksh: A part of variable A's name is inside of variable B, how to update A? pa3be Shell Programming and Scripting 4 03-30-2005 08:29 AM
using sed to replace a part of string csejl Shell Programming and Scripting 6 01-12-2004 08:48 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-18-2007
Registered User
 

Join Date: Jul 2007
Posts: 21
Repacing part of string with a variable

I have following strings in a file
DUPTASMTRMMBAL,20070416200704160117232101172321,,,,,,,@@@Y
DUPTASMTRMMCON,20070416200704160127189901271899,,,,,,,@@@Y
DUPTASMTRMMHG,,20070416200704160112051001120510,,,,,,,@@@Y

What i need to do is replace the date 20070416 with anoth date which is stored in variable enddate (enddate is result of certain calculations).

What i am tryning is this :-

for D in `cat file`;do
str1=`echo $D|cut -c1-15`
date1=`echo $D|cut -c16-23`
date2=`echo $D|cut -c24-31`
str2=`echo $D|cut -c32-`
enddate=$enddate
D=$str1$enddate$enddate$str2
done

This is not working.
Can someone please tell me an alternate method, using sed or awk. And also tell me why is this not working.
Reply With Quote
Forum Sponsor
  #2  
Old 07-18-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
sed "s/20070416/$enddate/g" file > temp
mv temp file
Code:
for D in `cat file`;do
str1=`echo $D|cut -c1-15`
date1=`echo $D|cut -c16-23`
date2=`echo $D|cut -c24-31`
str2=`echo $D|cut -c32-`
enddate=$enddate
echo $str1$enddate$enddate$str2 >> temp
done
mv temp file
Reply With Quote
  #3  
Old 07-18-2007
Registered User
 

Join Date: Jul 2007
Posts: 21
[quote=anbu23;302127633]
Code:
sed "s/20070416/$enddate/g" file > temp
mv temp file
This will not work for me, as i cannot give the string 20070416 and replace it with $enddate as this string is not fixed. It will keep changing everyday.

And sed "s/$date1/$enddate/g" file > temp does not work

Reply With Quote
  #4  
Old 07-18-2007
Registered User
 

Join Date: Jul 2007
Posts: 21
Code:
for D in `cat file`;do
str1=`echo $D|cut -c1-15`
date1=`echo $D|cut -c16-23`
date2=`echo $D|cut -c24-31`
str2=`echo $D|cut -c32-`
enddate=$enddate
echo $str1$enddate$enddate$str2 >> temp
done
mv temp file
[/quote]

I have already tried this option. For some reason, I am getting repeated data. What i mean is, if there are three rows in file then there are 39 rows in temp. The three rows are repeated 13 times.
I am not able to understand why is this happening.
Reply With Quote
  #5  
Old 07-18-2007
Registered User
 

Join Date: Jul 2007
Posts: 21
Code:
sed "s/20070416/$enddate/g" file > temp
mv temp file
Hey i tried again, the code sed "s/$date1/$enddate/g" works fine. I did some mistake earlier.
Thanks a lot for all your help!!!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0