0

Apologies but this question comes from a question previously raised: How can I convert a local date-time into UTC date-time? Toby has been absolutely great helping me with some conversion string-->BST -->GMT but ow i am facing another i believe dummy issue.

my .sh is simple:

# variables needed for testing
DateNew="20150903"
TimeNew="200001"

concatenating date and time

DateTimeNew_Suffix=${DateNew}${TimeNew}

storing the conversion provided by Toby Speight into a variable

newPrefix=echo "${DateTimeNew_Suffix}"| sed -re 's/^([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2})$/\1\\ \2:\3:\4/'| xargs date +@%s -d | xargs date -u +%Y%m%d%H%M%S -d

#printing that value echo $newPrefix

expected would be: "20150903190001" which should be the value assigned as a String into $newPrefix however Unix itself is throwing this exception:

date: extra operand 20:00:01' Try date --help' for more information.

date: option requires an argument -- 'd' Try `date --help' for more

information.

Running the command on its own i get the output expected:

echo "20150903200001"    | sed -re 's/^([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2})$/\1\\ \2:\3:\4/'    | xargs date +@%s -d    | xargs date -u +%Y%m%d%H%M%S -d

20150903190001

how can I store this echo into the newPrefix variable as a string? it look like conversion date into the variable is failing but the echo is not.

any help?

Franco
  • 167
  • Solution found :) DateNew='20150903' TimeNew='200001' DateTimeNew_Suffix=${DateNew}${TimeNew} newPrefix=$(sed -n 1p $filename | sed -re 's/^([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2})$/\1\ \2:\3:\4/'| xargs date +@%s -d| xargs date -u +%Y%m%d%H%M%S -d) echo $newPrefix echo "$newPrefix" – Franco Sep 03 '15 at 22:08
  • i had to use $() to store the actual sed instead echo - as i was triyng to pull it from a file itself.... but please if someone has a more elegant solution please do let me know...so happyyyyyy!!!!! i figured out – Franco Sep 03 '15 at 22:09
  • if you answer your own question, you should write up an answer and accept it – gwillie Sep 07 '15 at 07:38
  • I 'll do it hwillie – Franco Sep 07 '15 at 12:30

1 Answers1

-1

Solution found :)

DateNew='20150903'
TimeNew='200001'
DateTimeNew_Suffix=${DateNew}${TimeNew}
newPrefix=$(sed -n 1p $filename | sed -re 's/^([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2})$/\1\\ \2:\3:\4/'| xargs date +@%s -d| xargs date -u +%Y%m%d%H%M%S -d)
echo $newPrefix
echo "$newPrefix"
Franco
  • 167