To read timestamp and count from a line | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

To read timestamp and count from a line

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-12-2013
Abhisrajput Abhisrajput is offline
Registered User
 
Join Date: May 2013
Last Activity: 26 March 2014, 8:28 AM EDT
Location: Mumbai
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
To read timestamp and count from a line

I have file like:

Code:
Date and Time: 2013-05-11 12:23:12 MST
abc,1234,hi-all,45354-88888,IN,US
XYZ,1234,hi-all,45354-88888,OUT,GB
abc,1234,hi-all,45354-88888,IN,AS
abc,1234,hi-all,45354-88888,OUT,US
abc,1234,hi-all,45354-88888,IN,US
Number of Records: 000005


And i want to read the Timestamp from first line and count from the last line.

please help me out
Moderator's Comments:
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-12-2013 at 11:24 AM.. Reason: code tags, please!
Sponsored Links
    #2  
Old 05-12-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 19 September 2014, 1:56 PM EDT
Location: Galactic Empire
Posts: 3,383
Thanks: 234
Thanked 1,208 Times in 1,134 Posts

Code:
$ awk 'NR==1{T=$(NF-2)" "$(NF-1)}END{print T,$NF}' file
2013-05-11 12:23:12 000005

Sponsored Links
    #3  
Old 05-12-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 19 September 2014, 8:48 PM EDT
Location: India
Posts: 1,827
Thanks: 25
Thanked 524 Times in 503 Posts

Code:
sed -n '/^Date\|^Number/p' file

    #4  
Old 05-12-2013
hanson44 hanson44 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 12 May 2013, 11:33 PM EDT
Posts: 858
Thanks: 18
Thanked 180 Times in 177 Posts

Code:
$ sed -n -e "s/Date and Time: \(.*\) MST/\1/p" -e "s/Number of Records: *//p" input
2013-05-11 12:23:12
000005

The Following User Says Thank You to hanson44 For This Useful Post:
Abhisrajput (05-14-2013)
Sponsored Links
    #5  
Old 05-16-2013
Abhisrajput Abhisrajput is offline
Registered User
 
Join Date: May 2013
Last Activity: 26 March 2014, 8:28 AM EDT
Location: Mumbai
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
I have to read only the date from the first line. => 2013-05-11

Please help me out
Sponsored Links
    #6  
Old 05-16-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts

Code:
awk 'NR==1 {f=$4} {l=$NF} END {print f,l}' input
2013-05-11 000005

Sponsored Links
    #7  
Old 05-16-2013
xbin xbin is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 14 September 2014, 9:02 AM EDT
Posts: 105
Thanks: 5
Thanked 20 Times in 20 Posts
A variation of Hanson44's solution:

Code:
sed -n 's/Date and Time: \(.*\) \(.*\) .../\1/p;s/Number of Records: *//p' infile

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To read timestamp from a line Abhisrajput Shell Programming and Scripting 2 05-12-2013 09:33 AM
Read directories sequential based on timestamp chetan.c Shell Programming and Scripting 7 05-23-2012 12:40 AM
how to read the contents of two files line by line and compare the line by line? mjavalkar Shell Programming and Scripting 4 04-09-2012 12:25 AM
Shell script to count number of ~ from each line and compare with next line Ganesh Khandare Shell Programming and Scripting 3 09-29-2010 04:06 PM
bash: read file line by line (lines have '\0') - not full line has read??? alex_5161 Shell Programming and Scripting 6 04-14-2010 04:58 AM



All times are GMT -4. The time now is 10:00 AM.