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' Trydate --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?

  • 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 3 '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 3 '15 at 22:09
  • if you answer your own question, you should write up an answer and accept it – gwillie Sep 7 '15 at 7:38
  • I 'll do it hwillie – Franco Sep 7 '15 at 12:30
0

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"
  • You've introduced an unexplained new $filename variable. What is this? – Kusalananda Jan 13 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.