How do I pipe output to date -d "value"?
Solution 1:
gmt="$(grep "something" logfile.txt | grep "Succeeded" | cut -f1 -d'[')"
date -d "$gmt"
Or, if you prefer the pipeline format:
grep "something" logfile.txt | grep "Succeeded" | cut -f1 -d'[' | { read gmt ; date -d "$gmt" ; }
The problem is that date
does not use stdin. Thus, we have to capture the stdin into a variable (called gmt
here) and then supply that on the command line to date
.
Sample output from the second approach:
$ echo "2014-01-30 05:04:27 GMT" | { read gmt ; date -d "$gmt" ; }
Wed Jan 29 21:04:27 PST 2014
Solution 2:
If you're using GNU date from a sufficiently recent coreutils, there's date -f
, from the help screen:
-f, --file=DATEFILE like --date once for each line of DATEFILE
So your attempt 4 could have been:
$ grep "something" logfile.txt | grep "Succeeded" | cut -f1 -d'[' | date -f -
the last -
stands for stdin.