LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-02-2011, 03:00 PM   #1
mystupidquestion
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Rep: Reputation: Disabled
Converting the contents of a column with the output of the date command using awk


Hello,
I have a beginner text processing/bash/awk question. I have a text file of several columns, the output of sar. Here is a sample:

01:50:01 PM all 2.01 0.00 0.57 1.08 0.00 96.33
02:00:01 PM all 0.55 0.00 0.24 0.16 0.00 99.05
02:10:01 PM all 0.22 0.00 0.17 0.16 0.00 99.45
02:20:02 PM all 0.43 0.91 1.59 3.08 0.00 93.99


Column one has the time in the 12 hour format. I need the format of each item in column one to be in the 24 hour format. I know the date command will convert this for me:

date --date="07:40:01 PM" +%T

Returns: 19:40:01

I *think*, awk is the tool I need to use here. My game plan is: for each line, get the value of column 1. Run the value through the date command, and output the result plus the rest of the line to standard out or a file.
I've gotten awk to hand me the time in 24 hour format, but I'm stuck on using the date command on the returned field within awk. How do I do this? I looked in awk's man page, and I see there is a system function, but it returns the exit value. I want whats on standard out, not exit status, and it want to append other colums to the output. Any pointers/advice is appreciated.
 
Old 10-02-2011, 03:11 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Code:
awk '{("date --date=" $1 $2 " +%T") | getline time; print time}' file
This uses 4.9.6 Using getline into a Variable from a Pipe from the GNU awk manual.
 
1 members found this post helpful.
Old 10-02-2011, 03:33 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Slight modification:

Code:
awk '{("date --date=" $1 $2 " +%T") | getline $1;$2=""; print $0}' file
13:50:01  all 2.01 0.00 0.57 1.08 0.00 96.33
14:00:01  all 0.55 0.00 0.24 0.16 0.00 99.05
14:10:01  all 0.22 0.00 0.17 0.16 0.00 99.45
14:20:02  all 0.43 0.91 1.59 3.08 0.00 93.99


Cheers,
Tink
 
1 members found this post helpful.
Old 10-02-2011, 04:06 PM   #4
mystupidquestion
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Great. So this means....

Thanks, so, am I correct in interpreting this statement:
awk '{("date --date=" $1 $2 " +%T") | getline $1;$2=""; print $0}' file

means:
For each line, send the first and second column's (time plus AM/PM) entry to date. Pipe the date command's output to get line, and assign the result to column 1, right? Then, for the value of column 2 (AM or PM) set to a null string. Is $field="" the preferred way to remove a column? Are the other columns shifted left when you do this?.Are there not two sets of field separators when you set $2 to ""? Does it even matter? Lastly, print $0 is telling awk to print the current line, after your modifications.

Thanks!
 
Old 10-02-2011, 04:22 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by mystupidquestion View Post
Thanks, so, am I correct in interpreting this statement:
awk '{("date --date=" $1 $2 " +%T") | getline $1;$2=""; print $0}' file

means:
For each line, send the first and second column's (time plus AM/PM) entry to date. Pipe the date command's output to get line, and assign the result to column 1, right? Then, for the value of column 2 (AM or PM) set to a null string. Is $field="" the preferred way to remove a column? Are the other columns shifted left when you do this?.Are there not two sets of field separators when you set $2 to ""? Does it even matter? Lastly, print $0 is telling awk to print the current line, after your modifications.

Thanks!
You got that all right.

You gain one space from the $2="", but from awk's perspective that won't matter,
as it treats any number of any whitespace (spaces & tabs) as separator by default.

If you're concerned you could explicitly print all fields you need
individually. There's always more than one way to do it ;}
Code:
awk '{("date --date=" $1 $2 " +%T") | getline time;print time" "$3" "$4" "$5" "$6" "$7" "$8" "$9}' file
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk not parse the output of nc command! mythcat Linux - General 6 09-30-2011 03:00 AM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM
awk gsub() command - string (column) manipulation - substitution casperdaghost Linux - Newbie 1 03-08-2010 02:12 AM
command to sort by floats in fourth column of standard output jhwilliams Linux - Software 4 06-22-2007 01:27 PM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM


All times are GMT -5. The time now is 09:19 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration