One Liner sed date string to unixtime

datesed

I'm running the following command:

cat /tmp/myfile.log | sed -n -e 's/^.*Expire: //p'

Which returns the following string

04-nov-2018

I then want to run that string through the following command:

date -d '04-nov-2018' + "%s"

that will return the unixtime:

1541304000

How do I run that in a single command line?

Best Answer

You can run a command within a $( ), so that you can directly say:

date -d "$(command)" + "%s"

In your case:

date -d "$(sed -n -e 's/^.*Expire: //p' /tmp/myfile.log)" + "%s"

Note also I am saying sed '...' file instead of cat file | sed '...', since sed can directly read from the file.

Related Question