[SOLVED] Read second line from file with awk and use as variable.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Slackware Current - RedHat Enterprise
Posts: 16
Rep:
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:
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:
Distribution: Slackware Current - RedHat Enterprise
Posts: 16
Original Poster
Rep:
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.
Distribution: Slackware Current - RedHat Enterprise
Posts: 16
Original Poster
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.