Repacing part of string with a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Repacing part of string with a variable
# 1  
Old 07-19-2007
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. Smilie
# 2  
Old 07-19-2007
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  
Old 07-19-2007
[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

Smilie
# 4  
Old 07-19-2007
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.
# 5  
Old 07-19-2007
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!!!
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

String variable as part of expression in find command

Hi, I am new in scripting, and I am currently working on a script that will look for other files in a certain directory and exclude some file type. this works fine:Find_File2Exclude=`find ${paths} -maxdepth 1 -type f \( ! -iname '*.out' ! -iname '*.auc' ! -iname '*.cps' ! -iname '*.log' ! -iname... (4 Replies)
Discussion started by: kedd05
4 Replies

3. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

4. Shell Programming and Scripting

Using the part of a line as a variable?

Hello Friends, I need a command (or script line) that allows me to use of a part of line (given by me) as a variable. Let us assume the name of the command is MYCMD. When I type MYCMD fish://mfong@vhl.gov.nd/homefolder/hhk/ADS/ it must do the following job cd /homefolder/hhk/ADS/ ... (18 Replies)
Discussion started by: rpf
18 Replies

5. Shell Programming and Scripting

Need to take one part from a string

I have a string something like "/opt/src/default.cfg" OR /opt/src/common/one This whole string stored in an array. The problem is this string is not constant and it will keep on changing as lot of strings are stored in the array and it will be look like :- case 1 /opt/src/default.cfg ... (8 Replies)
Discussion started by: Renjesh
8 Replies

6. Shell Programming and Scripting

How read the part of the string into a variable?

Hi, I'm using bash and brand new to shell script. I would like to do the following. I have a string which is "UPDATE=1.0". I would like to read the value "1.0" alone in a variable. i.e the things afer "=" How do I do that? Thanks, (1 Reply)
Discussion started by: scriptfriend
1 Replies

7. Shell Programming and Scripting

Part of a string

Hi mates, I am doing a script in ksh. I have the following string: /opt/one/two/four/five/myFile.txt And I have a variable: echo "${variable}" -> /opt/one/two/ I would like to have just the string: four/five/myFile.txt What is the better way to do that? Thanks in... (3 Replies)
Discussion started by: gonzaloron
3 Replies

8. Shell Programming and Scripting

Copy part of a variable

Hi, i was using a input file to get the last line of the file.But now i have stored the values from the file to a variable and want the last line from the variable . Slightly confused on how to extract that data from the variable. previous code, cat input.txt <TIME>00:15:48</TIME>... (2 Replies)
Discussion started by: Shellslave
2 Replies

9. Shell Programming and Scripting

Getting part of a string

Hi, I have a string assinged to a varaible as below. FILE=/var/adm/message If $FILE is the value where it stores the path of message file. I would like to extract the location of the file message as below LOCATION=/var/adm FILE may have value like /var/adm/xxxx/message ... (2 Replies)
Discussion started by: raghu.amilineni
2 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question