Reputation: 1341
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
Upvotes: 0
Views: 137
Reputation: 30
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');
Upvotes: 0
Reputation: 84531
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');"
Upvotes: 0
Reputation: 784948
You can use:
echo "face_date:=TO_DATE($(cut -d '=' -f2 <<< "$str")),'YYYY/MM/DD');"
OUTPUT:
face_date:=TO_DATE( '20130612'),'YYYY/MM/DD');
Upvotes: 1