Format Time with Awked Output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format Time with Awked Output
# 1  
Old 05-31-2010
Format Time with Awked Output

Hi,

I would like the 2nd column in this log file to display as %k%S(00:00) in a new file. See my script and desired output below.


HTML Code:
05-25-2010 21:45:49 1616 1615 0 0 0 0 0 0 0
05-23-2010 21:55:49 1495 1493 0 0 0 3 0 0 0
05-23-2010 22:05:49 1750 1750 0 0 0 0 0 0 0
05-24-2010 22:15:49 1839 1837 0 0 0 0 1 0 0
05-24-2010 22:25:49 1693 1687 0 0 0 3 1 0 1
05-25-2010 22:35:49 1614 1609 0 0 0 3 4 0 0
05-25-2010 22:45:49 1599 1594 0 0 0 0 3 0 0
This is my current script.
HTML Code:
#!/bin/bash
echo "You must type the date in this format ---> MM-DD-YYYY"
read vdate
grep $vdate /log/log.csv |awk -F, '{print $2, $3, $12, $13, $14, $15, $16, $17,$18}' |tee /tmp/bk_$vdate.csv
Desired Output
HTML Code:
21:45 1616 1615 0 0 0 0 0 0 0
21:55 1495 1493 0 0 0 3 0 0 0

Smilie
# 2  
Old 05-31-2010
Try:
Code:
#!/bin/bash
echo "You must type the date in this format ---> MM-DD-YYYY"
read vdate

awk -v dat=$vdate '$0 ~ dat{$1="";sub("...$","",$2);sub("^ ","")}1' file


Last edited by Franklin52; 05-31-2010 at 06:21 AM.. Reason: Misread question
This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 05-31-2010
you are using field separator as comma (-F,) but your file doesn't seems to be csv file!!

don't you want the date field?

try:

Code:
awk -v dd="$vdate" '/dd/ {$2=substr($2,1,5);for (i=2;i<=NF;i++) printf "%s ",$i}' /log/log.csv |tee /tmp/bk_$vdate.csv

This User Gave Thanks to clx For This Post:
# 4  
Old 05-31-2010
Quote:
Originally Posted by anchal_khare
you are using field separator as comma (-F,) but your file doesn't seems to be csv file!!
Yeah, I also wondered about that...

@ravzter
However, no matter if it's comma separated or whitespace separated, there are no fields $12-$18 Smilie

If it's comma separated, try to replace your existing awk command in the script with this:
Code:
awk -F, '{print $2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' | sed 's/:[0-9][0-9] / /g'

or if it's whitespace separated, with this:
Code:
awk '{print $2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' | sed 's/:[0-9][0-9] / /g'

This User Gave Thanks to pseudocoder For This Post:
# 5  
Old 05-31-2010
sed

Code:
read -p "You must type the date in this format ---> MM-DD-YYY" vdate
grep $vdate log.csv | sed -e "s/^$vdate //" -e 's/\:[0-9][0-9] / /' | tee /tmp/bk_$vdate.csv

This User Gave Thanks to frans For This Post:
# 6  
Old 05-31-2010
Thanks everyone that replied, they all produce output of the logfile, however the Works script below gives me the desired TIME format.

Below is a snippet of the actual log file saved as csv.
I need to extract the underline.
HTML Code:
02-13-2007  14:38:11,2296,2281,2289,2335,2391,2392,2307,2307,0,288,0,0,0,8,8,46,0
02-13-2007 14:48:11,2058,2052,2055,2055,2043,2044,2043,2043,0,0,0,0,0,3,0,0,0
02-13-2007  14:58:11,2255,2242,2247,2247,2082,2081,2082,2082,0,0,0,0,0,5,9,0,0
The code below indicates the fields I need output for a specific excel report.
Works
Code:
grep $vdate log.csv |awk -F, '{print $1, $2, $3, $12, $13, $14, $15, $16, $17,$18}' | sed 's/:[0-9][0-9] / /g' |tee /tmp/bk_$vdate.csv

Output of Works
05-27-2010 23:45 1351 1351 0 0 0 0 0 0 0

However how do I eliminate the DATE portion highlighted in Red?
# 7  
Old 05-31-2010
Code:
s/.*-[0-9]\{4\} //'

It works as following: deletes everything before the last minus sign followed by 4 digits.

Try to improve your already existing sed part like this:
Code:
sed 's/:[0-9][0-9] / /g;s/.*-[0-9]\{4\} //'


Last edited by pseudocoder; 05-31-2010 at 10:25 AM..
This User Gave Thanks to pseudocoder For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate Time diff in milli milliseconds(Time format : HH:MM:SS,NNN)

Hi All, I have one file which contains time for request and response. I want to calculate time difference in milliseconds for each line. This file can contain 10K lines. Sample file with 4 lines. for first line. Request Time: 15:23:45,255 Response Time: 15:23:45,258 Time diff... (6 Replies)
Discussion started by: Raza Ali
6 Replies

2. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

3. Shell Programming and Scripting

time format

Hello Guys. I have copied the following from the time man pages time -f "%E real,%U user,%S sys" ls -Fs But I am getting -f: command not found Regards (3 Replies)
Discussion started by: fdc2suxs
3 Replies

4. Shell Programming and Scripting

calculate time from a given format

Hi i have a file which consists of the time records in following format H:MM:SS.sss 0:00:09.249 0:00:00.102 0:00:00.105 0:00:08.499 0:00:08.499 0:00:06.980 0:00:04.249 0:00:05.749 0:00:00.108 0:00:00.107 0:00:03.014 0:00:00.000 I need to calculate their equivalent milliseconds... (3 Replies)
Discussion started by: vaibhavkorde
3 Replies

5. Shell Programming and Scripting

Taking 3 greped/awked lines of text

Say I used grep and awk for taking text out of a text file... I now /have 3 lines of text that I want to combine to make 1 line. What command could I use to do this? ... display line 1 twice then sed s/$line1/$line2/ ????... display line 2 twice then sed s/$line2/$line3/ (2 Replies)
Discussion started by: puttster
2 Replies

6. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

7. Shell Programming and Scripting

capturing output from top and format output

Hi all, I'd like to capture the output from the 'top' command to monitor my CPU and Mem utilisation.Currently my command isecho date `top -b -n1 | grep -e Cpu -e Mem` I get the output in 3 separate lines.Tue Feb 24 15:00:03 Cpu(s): 3.4% us, 8.5% sy .. .. Mem: 1011480k total, 226928k used, ....... (4 Replies)
Discussion started by: new2ss
4 Replies

8. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies

9. Shell Programming and Scripting

help in time format

how to grep 30 mins from starting time of script let for example,script runs at 02:00am....i want to grep from 01.30 till 02:00 ------------------------------------------------------------------- how to achive this in a k-shell script ? (3 Replies)
Discussion started by: ali560045
3 Replies

10. Shell Programming and Scripting

time format..

HI.. I have some files...when doing "ls -l" its like this.. -rwxr-xr-x 1 e2e e2e 747 Aug 30 15:18 abc.txt how can I get the number YYYYMMDD from this...( since I need to compare this number with some other value..) with the help of date/awk/sed/epoch or whatever u... (1 Reply)
Discussion started by: clx
1 Replies
Login or Register to Ask a Question