Get last timestamp from file

Hi,

I have the following output in a file:

 NODE_NAME  DATE(GMT)  TIME_PERIOD         UNITS      TUPS 
---------         ----------      -----------              ------------ --------- 
slc-a              03/08/2016  08:45-08:50          94            0.3
                     03/08/2016 08:50-08:55           109          0.4
                     03/08/2016 08:55-09:00           71            0.2
                     03/08/2016 09:00-09:05           85            0.3
                     03/08/2016 09:05-09:10           55            0.2
                     03/08/2016 09:10-09:15           69            0.2
                     03/08/2016 09:15-09:20           85            0.3
                     03/08/2016 09:20-09:25           93            0.3
                     03/08/2016 09:25-09:30           81            0.3
                     03/08/2016 09:30-09:35           77            0.3
                     03/08/2016 09:35-09:40           73            0.2
                     03/08/2016 09:40-09:45           78            0.3 
*********   *********  *********  *********  *********                        
 slc-a               03/08/2016 08:45-08:50            9       0.0
                      03/08/2016 08:50-08:55            7       0.0
                      03/08/2016 08:55-09:00            7       0.0
                      03/08/2016 09:00-09:05            8       0.0
                      03/08/2016 09:05-09:10            5       0.0
                      03/08/2016 09:10-09:15            5       0.0
                      03/08/2016 09:15-09:20            3       0.0
                      03/08/2016 09:20-09:25            4       0.0
                      03/08/2016 09:25-09:30            8       0.0
                      03/08/2016 09:30-09:35            8       0.0
                      03/08/2016 09:35-09:40            3       0.0
                      03/08/2016 09:40-09:45            4       0.0 
*********                        ------------ --------- 

I am trying to capture the last timestamps from the first section and also from the last, i.e.

03/08/2016 09:40-09:45 

to be able to get the UNITS results, so basically my answer should be:

 03/08/2016 09:40-09:45           78
03/08/2016 09:40-09:45            4

Honestly I don't know from where I can start if I try using awk or sed. Can you help out?

Hello nms,

Considering that as per your shown Input_file your Input_file will be in sorted(increasing) order of time stamp and you will have ********* lines before each section starts. If yes to both of above mentioned conditions then following may help you in same.

awk '/^\*\*/ && Q{gsub(/^[[:space:]]+|[[:space:]]+$/,X,Q);print Q;Q="";next} {$NF="";Q=$0}'   Input_file

Output will be as follows.

03/08/2016 09:40-09:45 78
03/08/2016 09:40-09:45 4

Thanks,
R. Singh

1 Like
awk 'l && /^[*]/ {print l} NF {NF=NF-1; l=$0}' infile
1 Like

Hi,

Strangely enough when trying both codes I am returned with this error:

awk: syntax error near line 1
awk: bailing out near line 1
bash-4.1$ awk 'l && /^
[*]/ {print l} NF {NF=NF-1; l=$0}' voice.call.txt
awk: syntax error near line 1
awk: bailing out near line 1
bash-4.1$ awk 'l && /^
[*]/ {print l} NF {NF=NF-1; l=$0}' voice.call.txt
awk: syntax error near line 1
awk: bailing out near line 1

If your input file could contain more than two groups of data and you still just want the last entries from the first and last group (instead of the last entry from each group as produced by the code suggested RavinderSingh13 and rdrtx1), you could try:

awk '
/^[[:space:]]/ {	last_ts = $1 " " $2 "\t" $3 }
/^[*]/ && ! n++ {	print last_ts }
END {			print last_ts }
' file

which, with your sample data produces the output:

03/08/2016 09:40-09:45	78
03/08/2016 09:40-09:45	4

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk .

1 Like

Hello nms,

On a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

Thanks,
R. Singh

1 Like

Hi,

Yes the platform was Solaris. After inputting

/usr/xpg4/bin/awk

all work well (all 3 suggestions)

Many thanks for your input guys