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

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-12-2013
Abhisrajput Abhisrajput is offline
Registered User
 
Join Date: May 2013
Last Activity: 25 February 2015, 1:27 AM EST
Location: Mumbai
Posts: 21
Thanks: 5
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 Unix and Linux 05-12-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 27 March 2015, 2:02 PM EDT
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 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 Unix and Linux 05-12-2013
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 27 March 2015, 7:14 AM EDT
Location: India
Posts: 1,871
Thanks: 27
Thanked 536 Times in 513 Posts

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

    #4  
Old Unix and Linux 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 181 Times in 178 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 Unix and Linux 05-16-2013
Abhisrajput Abhisrajput is offline
Registered User
 
Join Date: May 2013
Last Activity: 25 February 2015, 1:27 AM EST
Location: Mumbai
Posts: 21
Thanks: 5
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 Unix and Linux 05-16-2013
Jotne's Unix or Linux Image
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 Unix and Linux 05-16-2013
xbin xbin is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 20 March 2015, 3:11 PM EDT
Posts: 115
Thanks: 5
Thanked 23 Times in 23 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 05:21 AM.