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
Need help regarding replacing a part of string veerapureddy Shell Programming and Scripting 1 03-17-2008 09:02 AM
how to get the last part of a string followed by a pattern bluemoon1 Shell Programming and Scripting 4 09-08-2007 12:10 PM
Extracting part of a string sam_78_nyc Shell Programming and Scripting 8 04-25-2007 08: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 12:29 PM
using sed to replace a part of string csejl Shell Programming and Scripting 6 01-12-2004 11:48 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-19-2007
divz divz is offline
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.
  #2 (permalink)  
Old 07-19-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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
  #3 (permalink)  
Old 07-19-2007
divz divz is offline
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

  #4 (permalink)  
Old 07-19-2007
divz divz is offline
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!!!
  #5 (permalink)  
Old 07-19-2007
divz divz is offline
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.
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 01:28 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