LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-23-2012, 06:14 AM   #1
pampers
LQ Newbie
 
Registered: Sep 2012
Location: Chile
Distribution: Slackware Current - RedHat Enterprise
Posts: 16

Rep: Reputation: Disabled
Smile Read second line from file with awk and use as variable.


Hello.

I have a file like this:

Code:
13:36
13:38
13:39
I have to read the last 2 lines and compare both times and print how much minutes or seconds had passed from each other. I have the following awk script:

PHP Code:
{

hora1=substr($1,1,2);
minuto1=substr($1,4,2);
#segundo1=substr($2,7,2);

hora2=substr($2,1,2);
minuto2=substr($2,4,2);
#segundo2=substr($3,7,2);

tiempo1=hora1*60*60 minuto1*60;
tiempo2=hora2*60*60 minuto2*60;

diff=tiempo1-tiempo2;

printf "%s %d:%d:%d\n","La diferencia es ",diff/(60*60),diff%(60*60)/60,diff%60;


which reads a file and print results without problem, if the file were like this:

13:36 13:38
13:40 13:45
13:50 13:55

It works fine this way, but how can I make awk read the second line and print results without problem? Since the file I want to read is in the following format:

13:36
13:38
13:40
13:45


Thanks in Advanced, for any help.
 
Old 11-23-2012, 06:24 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,950

Rep: Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308
something like this should work
Code:
awk ' NR % 2 == 0 { hora1=substr($1,1,2); minuto1=substr($1,4,2) }
      NR % 2 == 1 { hora2=substr($1,1,2); minuto2=substr($1,4,2) 

tiempo1=hora1*60*60 + minuto1*60;
tiempo2=hora2*60*60 + minuto2*60;

diff=tiempo1-tiempo2;

printf "%s %d:%d:%d\n","La diferencia es ",diff/(60*60),diff%(60*60)/60,diff%60;

 } ' textfile
 
1 members found this post helpful.
Old 11-23-2012, 06:45 AM   #3
pampers
LQ Newbie
 
Registered: Sep 2012
Location: Chile
Distribution: Slackware Current - RedHat Enterprise
Posts: 16

Original Poster
Rep: Reputation: Disabled
Hey cool it does work I really appreciate a lot your help.

I am extremely newbie in awk programming, so there,s a bunch of things I still need to learn and understand. Your modification does work very well

There,s a little inconvenience, maybe I have missed something here don,t know. Well I have a file (let's call it textfile.txt) with the following times let said:

13:30
13:35
13:50

etc, etc. When I execute your awk script like this:

bash$ awk -f myawkscript.awk < textfile.txt

The results I get back are as follow:

-13:30
0:15:0
0:15:0

How can I omit printing the first line from the textfile.txt? I just want the results from the times calculation.

Sorry if this is too newbie I really appreciate any help you can provide.
 
Old 11-23-2012, 06:57 AM   #4
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,950

Rep: Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308
I think I made a mistake, first you need NR % 2 == 1 and in the second line NR % 2 == 0.
 
Old 11-23-2012, 07:50 AM   #5
pampers
LQ Newbie
 
Registered: Sep 2012
Location: Chile
Distribution: Slackware Current - RedHat Enterprise
Posts: 16

Original Poster
Rep: Reputation: Disabled
Wink

Thanks Pan64 I made the change, but unfortunately it keeps printing the first line on stdout. What I did was, a bash script that do a tail command that read only the last 2 lines, so this way I avoid any below line. It works this way

I appreciate all your help in providing a solution for my request.

Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
[SOLVED] Looping using while read line, using a variable instead of a file. henrtm05 Programming 3 09-24-2010 11:49 PM
how-to make sed read 1 random line into a file and parse it ot a variable?? Speedy2k Linux - Newbie 7 05-24-2009 11:23 AM
[SOLVED] use awk &/or sed to read file 1 line 1 & file 2 line 1 gr8scot Linux - General 6 07-12-2007 08:13 PM
how to read certain line of file, and output to variable? jimmyjiang Red Hat 2 05-01-2007 08:46 AM


All times are GMT -5. The time now is 05:54 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration