LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-03-2011, 01:59 AM   #1
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Rep: Reputation: 30
How to pass multiline output of a command into 'date -j'


Hey folks

I am doing some NSCA log parsing and I want to get an output like this:

2011-Feb-18:11:00:07
2011-Feb-18:11:00:07
2011-Feb-18:11:00:07
2011-Feb-18:11:00:08
2011-Feb-18:11:00:08

converted to this:

1298026807
1298026807
1298026807
1298026808
1298026808

Of course I'm making it hard for myself by cramming it into a shell one liner - this is where I'm at:

The following will create the formatted-date output:

Code:
sort -b -k4.9,4.12 -k4.5b,4.7Mb -k4.2,4.3 -k4.14,4 foo.log| awk -F '[ \[\/:]' '{print $7"-"$6"-"$5":"$8":"$9":"$10}'
and I want to pass that into the second parameter of 'date -j' like this:

Code:
date -j -f "%Y-%b-%d:%T" "+%s" "<pass each line into here>"
I have tried this, but it treats the entire output as a single line, then plops a timestamp on the end (I think):

Code:
sort -b -k4.9,4.12 -k4.5b,4.7Mb -k4.2,4.3 -k4.14,4 foo.log| date -j -f "%Y-%b-%d:%T" "+%s" "`awk -F '[ \[\/:]' '{print $7"-"$6"-"$5":"$8":"$9":"$10}'`"
I'm pretty sure this is dead easy, but I just wasted an hour on it

Any ideas?

christo

Last edited by chr15t0; 03-03-2011 at 02:16 AM.
 
Old 03-03-2011, 02:14 AM   #2
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,557

Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
Use GNU Parallel

Code:
sort -b -k4.9,4.12 -k4.5b,4.7Mb -k4.2,4.3 -k4.14,4 foo.log| awk -F '[ \[\/:]' '{print $7"-"$6"-"$5":"$8":"$9":"$10}' | parallel date -j -f "%Y-%b-%d:%T" "+%s"
 
Old 03-03-2011, 02:25 AM   #3
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,557

Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
Actually you could do this with xargs but it is uglier:

Code:
sort -b -k4.9,4.12 -k4.5b,4.7Mb -k4.2,4.3 -k4.14,4 foo.log| awk -F '[ \[\/:]' '{print $7"-"$6"-"$5":"$8":"$9":"$10}' | xargs -I {} date -j -f "%Y-%b-%d:%T" "+%s" {}
I'd still recommend you install Parallel. It will make life easier later on as it is much more powerful.

Last edited by ruario; 03-03-2011 at 02:30 AM. Reason: removed '-E' as it isn't needed
 
Old 03-03-2011, 04:22 AM   #4
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Original Poster
Rep: Reputation: 30
two great responses. Thanks guys


christo
 
Old 03-03-2011, 04:42 AM   #5
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,557

Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
Well I am just one guy, but you are welcome.
 
Old 03-06-2011, 12:15 PM   #6
tange
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Rep: Reputation: 9
I am not really sure what you do by the sorting. Also 'date -j' seems not to be part of 'date (GNU coreutils) 8.5'.

But the following gives the output you have shown for the input you have shown:
Code:
cat foo.log | parallel -k --colsep '-|:' date +%s --date "'{2} {3} {1} {4}:{5}:{6}'"
It will spawn date for each line, so if you have a lot of lines, you might consider using perl with Time::Local.
 
Old 03-06-2011, 01:21 PM   #7
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,557

Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
Quote:
Originally Posted by tange View Post
Also 'date -j' seems not to be part of 'date (GNU coreutils) 8.5'.
If you look at his icon it is from a Mac, so he is using FreeBSD date.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need date command to output in format 08-Dec-2010 Glenn D. Linux - Software 3 12-09-2010 11:13 AM
[SOLVED] Subshell multiline output timbCFCA Linux - Server 4 09-01-2010 08:46 AM
Pass output of one command as input to another on bash linuxlover.chaitanya Linux - Newbie 3 01-08-2009 01:46 AM
how to pass pipe output to file command nickleus Linux - General 6 06-19-2008 06:18 AM
Capture partial output from `date` command tekmann33 Linux - Newbie 7 12-27-2006 01:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration