Assigning a variable

I have a date column as 06302015 but I need to have variable which extracts 063015.

Am trying something like below but it is not assigning

Please let me know if am missing something. Thanks in advance.

################################

 #!/usr/bin/ksh
 
DT=06302015

 DT_ETL= echo "$DT" | cut -c1,2,3,4,7,8
 
EXTENSION="$DT_ETL"
  
 exit 0

################################

  1. Avoid spaces around =

  2. DT_ETL=$(echo "$DT" | cut -c1,2,3,4,7,8)

First: CODE tags are VERY important. Without them, we couldn't see that you didn't have the #! on the 1st line of your script at the start of the line. With the #! following a space, it is just a comment and your script will be run by the default shell on your system; not /usr/bin/ksh .

And, if you're using the Korn shell (or any shell that performs the variable expansions required by the POSIX standards), you can run this faster using shell expansions instead of invoking an external utility ( cut ):

#!/usr/bin/ksh

DT=06302015
mmdd=${DT%????}
yy=${DT#??????}
EXTENSION="$mmdd$yy"
printf '%s\n' "$EXTENSION"
exit 0

which prints:

063015

instead of just setting a variable that can't be used after your script exits.

You could also use

printf "%s\n" ${DT:0:4}${DT:6:2}
063015

With bash or a 1993 or later ksh ; yes. But, the substring extraction parameter expansions are not required by the standards and do not appear in nearly as many shells as the ${var#pattern} and ${var%pattern} expansions.

Hi All,

Thanks for the solutions.

Am able to extract the expected value but not able to assign to another variable. When I assign to another variable it becomes blank.

Even I tried with printf and it is also not working.

#!/usr/bin/ksh

DT=06302015

DT_ETL=$(echo "$DT" | cut -c1,2,3,4,7,8) 

EXTENSION="$DT_ETL"

exit 0

Output:

ksh -x test.ksh 06302015
+ DT=06302015
+ echo 06302015
+ cut -c1,2,3,4,7,8
+ 063015
+ DT_ETL=
+ EXTENSION=

Difficult to believe ...

DT=06302015
+ DT=06302015
DT_ETL=$(echo "$DT" | cut -c1,2,3,4,7,8) 
echo "$DT" | cut -c1,2,3,4,7,8
++ echo 06302015
++ cut -c1,2,3,4,7,8
+ DT_ETL=063015

Are you sure you're showing the entire unmodified code snippet?

Thanks for the reply.

It's working fine now. Thanks for all the solutions.