linux - How do I pipe output to date -d "value"?
2014-04
I have a date like "2014-01-30 05:04:27 GMT", and if I run date -d "2014-01-30 05:04:27 GMT", the output is in my server's timezone ("Thu Jan 30 16:04:27 EST 2014").
With the use of grep and cut, I have extracted the date in GMT from a file. However, I am struggling to then convert this into my local time.
For example:
[user@server log]# grep "something" logfile.txt | grep "Succeeded" | cut -f1 -d'['
Output: 2014-01-30 05:04:27 GMT
What can I add on the end, to pass that output to "date -d"?
Attempted:
- [user@server log]# grep "something" logfile.txt | grep "Succeeded" | cut -f1 -d'[' | date -d
- [user@server log]# grep "something" logfile.txt | grep "Succeeded" | cut -f1 -d'[' | date
- [user@server log]# grep "something" logfile.txt | grep "Succeeded" | cut -f1 -d'[' | date -d "$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
I'm trying to grep the output of ngrep. Unfortunately when I add another grep to the pipeline, I get no output at all. It can be some other command too - cat / grep / tee - everything breaks the chain. Example:
# this works:
$ ngrep -l -q -T -Wbyline -d any udp and port 5060 |
egrep -B1 '^SIP/2.0 180'
--
U +1.469535 xxx:5060 -> xxx:5060
SIP/2.0 180 Ringing.
--
U +0.001384 xxx:5060 -> xxx:2048
SIP/2.0 180 Ringing.
but
#these don't:
$ ngrep -l -q -T -Wbyline -d any udp and port 5060 |
egrep -B1 '^SIP/2.0 180' | egrep '^U'
$ ngrep -l -q -T -Wbyline -d any udp and port 5060 |
egrep -B1 '^SIP/2.0 180' | cat
$ ngrep -l -q -T -Wbyline -d any udp and port 5060 |
egrep -B1 '^SIP/2.0 180' | tee test
If I use cat somefile
instead of ngrep at the start, everything works as expected.
Any ideas what could go wrong here?
Have you got egrep aliased to anything that might modify the text, like grep --color
might do? $ type egrep
Also check the output for "hidden" control characters using od -bc
(as jch mentioned) or hd -C
.
EDIT: I just ran into this myself with 'egrep' - was doing:
tail -f somefile|egrep 'somepattern'
Which would provide gratifying output every two seconds; but the following produced nothing:
tail -f somefile|egrep 'somepattern'|tr -d '%'
After some digging in the man page I found the '--line-buffered' option which produced output again!
Then I found this description of buffering in pipelines - it looks like what it boils down to is that some command line utils (eg: tail -f) routinely call fflush on stdout and others (cut, grep, etc) do not.
My wrong first reply is below
Usually when I run into these types of issues, one or more of the programs in the pipeline are putting output on multiple file descriptors - most often STDERR.
You might try adding '2>&1' before your first '|' in the pipeline to redirect STDERR to STDOUT.