0

I am trying to format a shell out

str="set @face_date = '20130612' 

The below code returns the out as '20130612'

echo $str |  cut -d '=' -f2

But the following code is not working

echo "face_date:=TO_DATE("$str | cut -d '=' -f2"),'YYYY/MM/DD');"

Expected Result:

face_date:= to_date('2013-06-12','YYYY/MM/DD');

Thanks in advance for your help

2
  • You're nesting double-quotes inside other double-quotes. Try escaping them with backslashes. Commented Jun 25, 2014 at 21:40
  • perhaps try an embedded echo in backquote. not sure how newlines are handled though. except for that should work. like echo "face_date:=TO_DATE("`echo $str | cut -d '=' -f2`"),'YYYY/MM/DD');" disclaimer: haven't tried and i seldom use Unix-land shells. Commented Jun 25, 2014 at 21:40

4 Answers 4

1

You can use:

echo "face_date:=TO_DATE($(cut -d '=' -f2 <<< "$str")),'YYYY/MM/DD');"

OUTPUT:

face_date:=TO_DATE( '20130612'),'YYYY/MM/DD');
Sign up to request clarification or add additional context in comments.

Comments

0

One more way,

echo "face_date:=TO_DATE($(echo $str | cut -d '=' -f2)),'YYYY/MM/DD');"

This will produce following output.

face_date:=TO_DATE( '20130612'),'YYYY/MM/DD');

or you can use

str1=$(echo $str |  cut -d '=' -f2)
echo "face_date:=TO_DATE($(echo ${str1:0:6}-${str1:6:2}-${str1:8:3}),'YYYY/MM/DD');"

for following output

face_date:=TO_DATE('2013-06-12','YYYY/MM/DD');

Comments

0

Maybe that can help you. You need to include your command in ` (ASCII #96) in order to get executed. Perhaps that question is a better fit here though.

Comments

0

You need to either escape the inner double quotes, or surround in $() or ``:

echo "face_date:=TO_DATE($("$str | cut -d '=' -f2")),'YYYY/MM/DD');"

or simply get rid of all the pipes and cut, just do:

echo "face_date:=TO_DATE(${str##* }),'YYYY/MM/DD');"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.