Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and 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: 2 September 2015, 1:18 PM EDT
Location: Galactic Empire
Posts: 3,423
Thanks: 240
Thanked 1,225 Times in 1,151 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: 23 August 2015, 9:36 AM EDT
Location: India
Posts: 1,892
Thanks: 27
Thanked 539 Times in 516 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: 26 July 2015, 5:25 PM EDT
Posts: 124
Thanks: 8
Thanked 27 Times in 27 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

Linux 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 11:11 AM.