Passing a varaible to sed issue


 
Thread Tools Search this Thread
Operating Systems Solaris Passing a varaible to sed issue
# 1  
Old 06-09-2017
Passing a varaible to sed issue

I am unable to make it work. I tried dbl quotes ...etc. Nothing works. I am trying to replace value in an XML file to current system date/time. Here is my code:

Code:
NTIMESTAMP=`date '+%m%d%Y%H%M%S'`
 sed -e 's|<TIMESTAMP>[0-9a-z.]\{1,\}|<TIMESTAMP>$NTIMESTAMP|g' $QLOG
  
 Input data sample:
 <MSGTYPE>QI</MSGTYPE><TIMESTAMP>06062017120000</TIMESTAMP>
  
 Output with my code "Not working":
 <MSGTYPE>QI</MSGTYPE><TIMESTAMP>$NTIMESTAMP</TIMESTAMP>

What am I doing wrong? I tried using double quotes, when I do it blanks out the timestamp !

Please help. Thanks

---------- Post updated at 11:26 AM ---------- Previous update was at 11:16 AM ----------

I think I found the solution. Using this works perfectly:

Code:
eval "$(echo "sed -e 's|<TIMESTAMP>[0-9a-z.]\{1,\}|<TIMESTAMP>$NTIMESTAMP|g' $QLOG")"

# 2  
Old 06-09-2017
You say you tried double quotes, but see:
Code:
sed -e "s|<TIMESTAMP>[0-9a-z.]\{1,\}|<TIMESTAMP>$NTIMESTAMP|g" file
 <MSGTYPE>QI</MSGTYPE><TIMESTAMP>06092017182948</TIMESTAMP>

# 3  
Old 06-09-2017
Quote:
Originally Posted by RudiC
You say you tried double quotes, but see:
Code:
sed -e "s|<TIMESTAMP>[0-9a-z.]\{1,\}|<TIMESTAMP>$NTIMESTAMP|g" file
 <MSGTYPE>QI</MSGTYPE><TIMESTAMP>06092017182948</TIMESTAMP>

I tried that and did no work at all. But using this works:

Code:
eval "$(echo "sed -e 's|<TIMESTAMP>[0-9a-z.]\{1,\}|<TIMESTAMP>$N_TIMESTAMP|g' $QLOG")" > $QLOG.tmp

It seems to be the only way to make it work afterall. Thanks
# 4  
Old 06-09-2017
In what way did it "not work"? What did it actually do?

Whenever arguments start being finicky, its a good idea to print them back out to make sure you're putting in what you think you are. Try
Code:
printf "%s\n" "s|<TIMESTAMP>[0-9a-z.]\{1,\}|<TIMESTAMP>$NTIMESTAMP|g"

to make sure its doing what you think it is.

eval is definitely not "the way" to do this. That it's not working suggests to me the input might have things in it which are unexpected and eval works around by quote-splitting or some such.
# 5  
Old 06-09-2017
When I use SED with double " to take $NTIMESTAMP value, it gives me this:
Code:
 <MSGTYPE>QI</MSGTYPE><TIMESTAMP>$NTIMESTAMP</TIMESTAMP>

It is not passing the date value from the variable.

Only when I use eval, it works fine.

I ran your printf and I get this:
Code:
s|<TIMESTAMP>[0-9a-z.]\{1,\}|<TIMESTAMP>06092017131913|g

# 6  
Old 06-09-2017
Good, and if printf doesn't mess it up, neither will sed. The shell does not differentiate between the two, substitution happens before sed or printf is run.

Hit the up arrow on your console to repeat the 'printf' command, delete the 'printf "%s\n"', insert 'sed' in its place, go to the end, add '< inputfile > outputfile' and hit enter.

If that doesn't work, show exactly what you do and exactly what result you get, word for word, letter for letter, keystroke for keystroke. You're doing some substitution or escaping somewhere which causes $ to be taken literally. Notably, you can't store $ in a variable and expect it to be evaluated later.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Passing value of a variable in sed

Hi, I want to pass value of a variable track_line which is the line number to sed. Sed should print the lines starting from track_line till the last line of the file. I tried the below command but it is not working. sed -n '${track_line},$p' latest_log_file I tried using the below too but... (1 Reply)
Discussion started by: nitinupadhyaya8
1 Replies

2. Shell Programming and Scripting

Issue with passing variable to Grep in a shell script

Hi, I'm trying to check if methods specified in a class have been added to the corrosponding interface. My code below is giving me the following errors: grep: function: No such file or directory grep: import($zipfile): No such file or directory grep: function: No such file or... (1 Reply)
Discussion started by: racshot65
1 Replies

3. Shell Programming and Scripting

Issue in passing passwd to login into a sudo account

Hi Gurus, I have small issue... I used to pass the passwd for sudo commands like below, gzcat ~/passwd.gz | sudo su - <villin> >> eof ------ ----- ------ eof And it was able to login into "villin" sudo account successfully. But now, I'm using the same in another script for the... (2 Replies)
Discussion started by: raghu.iv85
2 Replies

4. Shell Programming and Scripting

Issue in passing a parameter value from one script to another

Hi Experts, I am passing current month & day (i.e. 'Jul 21') from script aaa.ksh to zzz.ksh. The value 'Mon DD' is being used in zzz.ksh. Problem: If I pass 'Mon DD' value manually to zzz.ksh i.e. /test/zzz.ksh 'Jul 21' it works fine. However, if I pass value from aaa.ksh, it does... (2 Replies)
Discussion started by: dipeshvshah
2 Replies

5. UNIX for Dummies Questions & Answers

ISSUE on SFTP fucntion ,parameter passing!

Hi Everyone!! Hey i created a SFTP function to FTP the file from unix to Linux. I need to FTP the 48 files from unix to linux. IP=$1 Userid=$2 Prikeypath=$3 SrcPath=$4 DstPath=$5 Files=$6 BATCHFILE=sftp.batch.$$ LOGFILE=sftp.log.$$ #Compose batch file & pass as argument to the... (1 Reply)
Discussion started by: bobprabhu
1 Replies

6. Shell Programming and Scripting

Passing Variable in sed

Dear All, I want to print a file. First I tried with this sed '2q;d' filename it worked. But when i put following it is not working x=2; sed '$xq;d' filename Would any one suggest how to pass the variable? (7 Replies)
Discussion started by: saifurshaon
7 Replies

7. Shell Programming and Scripting

Passing variables to sed

Hi Folks, How can I make the following to work from a korn shell? old="OLDSTRING" new="NEWSTRING" file="myfile.txt" sed -n 's/$old/$new/gp' $file Thanks in advance rogers42 (3 Replies)
Discussion started by: rogers42
3 Replies

8. UNIX for Dummies Questions & Answers

append space to a varaible using sed

Hi, I would like to append 100 blank spaces to a variable using sed. eg: var1=abcdef Could anyone advice??? thanks, Sumith (3 Replies)
Discussion started by: sam99
3 Replies

9. Shell Programming and Scripting

variable passing to sed

I m trying to pass variable to sed. export var=140920060731 sed -e '/$var/d' file but no luch so far..? any body has any idea abt it Is there any way to pass variable to SED? Thanks , Manish (2 Replies)
Discussion started by: Manish Jha
2 Replies

10. Shell Programming and Scripting

Passing variables to sed

Hi folks, I'm looking for a solution to pass variables to a sed-command. I'm reading a lot of threats and also the q&a "How can I use a variable in sed?". None of these commands works. I'm using AIX 5.2. I want to do the following: NUMBER=` echo 38341` | sed -n '/$NUMBER/p' an obtained... (3 Replies)
Discussion started by: jfisch
3 Replies
Login or Register to Ask a Question