![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
[quote=anbu23;302127633]
Code:
sed "s/20070416/$enddate/g" file > temp mv temp file And sed "s/$date1/$enddate/g" file > temp does not work |
|
#4
|
|||
|
|||
|
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 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. |
|
#5
|
|||
|
|||
|
Code:
sed "s/20070416/$enddate/g" file > temp mv temp file Thanks a lot for all your help!!! |
|||
| Google The UNIX and Linux Forums |