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?

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

1 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .