Extracting variables from echo

Hello all.

I can not remember the command to extract a variable from the date command.

Basically what I need to do is to store the values of date in variable and rearrange them. I can not remember the command or the syntax to do so.

so..

date
Mon Mar 8 06:57:19 GMT 2010
$1 $2 $3 $4 $5 $6

then write files like...

echo "$3$2$6 $4" >> file

I'm sure this is a simple solve... I keep wanting to go back to sed, but I know that's not right. I'm wanting to use /usr/sh

Thanks

are you looking for this?

date +'%d%b%S %H'

$ date +'%d%b%S %H'
08Mar03 07
$

Close. If there's a document or a page I could be referenced too, I should be able to find it from there.

Try this command.

date +'%d%b%Y %H'

For more details please refer man date

$ date +%e%b%Y
8Mar2010
$

Got it... now how do I chomp the beginning whitespace? I say chomp because I remember the perl variant.

---------- Post updated at 02:09 AM ---------- Previous update was at 02:08 AM ----------

I guess the e and the d meant the difference between the 0 and whitespace. For future reference though, how would I preform a chomp?

date +%d" "%b" "%Y" "%T

Or

date | awk '{print $3,$2,$6,$4}'
sed 's/^ //' <<< $(date +%e%b%Y)
sed 's/^ *//g'

This is used to remove the leading white spaces

try this,

date | awk ' { print $3,$2,$6,$4 } '

I guess chomp remove the trailing \n? !!

Ya, of course. So do you want to remove the trailing white space characters?

You can use the following code

declare -a array
declare -a array1
array1=(3,2,6,4);
count=1;
for i in `date| cut -d ' ' -f 1-8`
do
        array[$count]=$i;
        ((count++))
done

for j in `echo ${array1}|tr ',' ' '`
do
        echo ${array[${j}]} ;
done

You mentioned that, chomp because I remember the perl variant. chomp used to remove the trailing newline only.
If you want to remove the trailing newline try this,

sed -r 's/\n$//'  

(or)
sed -re 's/(.*)\n$/\1/'