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.
wondering if there was a way to grep my file that contains thousands of lines like this:
Quote:
Thu Feb 02 01:30:44 EST 2012,Thu Feb 02 01:31:04 EST 2012,169
Thu Feb 02 01:31:04 EST 2012,Thu Feb 02 01:31:24 EST 2012,206.4
Thu Feb 02 01:31:24 EST 2012,Thu Feb 02 01:31:44 EST 2012,146.2
Thu Feb 02 01:31:44 EST 2012,Thu Feb 02 01:32:04 EST 2012,134.35
taking out all lines that contain a time before 09:00:00 and after 17:00:00 ??
Tried using grep -v '(08:[0-5][0-9]:*|00:*:*)' but isnt working..
Managing, parsing and interpreting times and dates is tricky at the best of times. There are so many formats used, and the formats don't lend themselves well to sorting and other natural ordering. I doubt that a pure grep solution is practical. What other programming languages are you acquainted with?
--- rod.
If I understand the request, you want everything inside [09 - 17] hours, which is the other way of saying you want to omit everything outside of those hours.
If either one of the times is outside the bounds, you want to exclude it, correct? As it is now, it looks like all these lines would be excluded.
Please post enough of a sample that shows what should be included and excluded, and what you expect the results to be. Please post that sample inside CODE brackets, not inside QUOTE brackets.
... taking out all lines that contain a time before 09:00:00 and after 17:00:00 ??
I think awk can do the job.
As a newbie, my skill with awk is limited. A knowledgeable LQer can tighten up this code.
Input file:
Code:
Thu Feb 02 01:31:24 EST 2012,Thu Feb 02 06:31:44 EST 2012,146.2
Thu Feb 02 01:31:44 EST 2012,Thu Feb 02 07:32:04 EST 2012,134.35
Thu Feb 02 01:30:44 EST 2012,Thu Feb 02 08:31:04 EST 2012,169
Thu Feb 02 01:31:04 EST 2012,Thu Feb 02 09:31:24 EST 2012,206.4
Thu Feb 02 01:31:24 EST 2012,Thu Feb 02 10:31:44 EST 2012,146.2
Thu Feb 02 01:31:24 EST 2012,Thu Feb 02 11:31:44 EST 2012,146.2
Thu Feb 02 01:31:44 EST 2012,Thu Feb 02 12:32:04 EST 2012,134.35
Thu Feb 02 01:31:24 EST 2012,Thu Feb 02 13:31:44 EST 2012,146.2
Thu Feb 02 01:31:44 EST 2012,Thu Feb 02 14:32:04 EST 2012,134.35
Thu Feb 02 01:31:24 EST 2012,Thu Feb 02 15:31:44 EST 2012,146.2
Thu Feb 02 01:31:44 EST 2012,Thu Feb 02 16:32:04 EST 2012,134.35
Thu Feb 02 01:30:44 EST 2012,Thu Feb 02 17:31:04 EST 2012,169
Thu Feb 02 01:31:04 EST 2012,Thu Feb 02 18:31:24 EST 2012,206.4
Thu Feb 02 01:31:24 EST 2012,Thu Feb 02 19:31:44 EST 2012,146.2
Thu Feb 02 01:31:44 EST 2012,Thu Feb 02 20:32:04 EST 2012,134.35
Thu Feb 02 01:31:04 EST 2012,Thu Feb 02 09:31:24 EST 2012,206.4
Thu Feb 02 01:31:24 EST 2012,Thu Feb 02 10:31:44 EST 2012,146.2
Thu Feb 02 01:31:24 EST 2012,Thu Feb 02 11:31:44 EST 2012,146.2
Thu Feb 02 01:31:44 EST 2012,Thu Feb 02 12:32:04 EST 2012,134.35
Thu Feb 02 01:31:24 EST 2012,Thu Feb 02 13:31:44 EST 2012,146.2
Thu Feb 02 01:31:44 EST 2012,Thu Feb 02 14:32:04 EST 2012,134.35
Thu Feb 02 01:31:24 EST 2012,Thu Feb 02 15:31:44 EST 2012,146.2
Thu Feb 02 01:31:44 EST 2012,Thu Feb 02 16:32:04 EST 2012,134.35
yes i want to keep everything within the timeframe of 09:00:00 -> 17:00:00 anything outside of this get rid of.
Code:
Tue Feb 21 08:55:04 EST 2012,Tue Feb 21 08:55:24 EST 2012,324.7,
Tue Feb 21 08:55:24 EST 2012,Tue Feb 21 08:55:44 EST 2012,356.65,
Tue Feb 21 08:55:44 EST 2012,Tue Feb 21 08:56:04 EST 2012,528.5,
Tue Feb 21 08:56:04 EST 2012,Tue Feb 21 08:56:24 EST 2012,489.2,
Tue Feb 21 08:56:24 EST 2012,Tue Feb 21 08:56:44 EST 2012,471.8,
Tue Feb 21 08:56:44 EST 2012,Tue Feb 21 08:57:04 EST 2012,281.6,
Tue Feb 21 08:57:04 EST 2012,Tue Feb 21 08:57:24 EST 2012,344.45,
Tue Feb 21 08:57:24 EST 2012,Tue Feb 21 08:58:04 EST 2012,430.05,
Tue Feb 21 08:58:04 EST 2012,Tue Feb 21 08:58:24 EST 2012,360.05,
Tue Feb 21 08:58:24 EST 2012,Tue Feb 21 08:58:44 EST 2012,384.75,
Tue Feb 21 08:58:44 EST 2012,Tue Feb 21 08:59:04 EST 2012,446.55,
Tue Feb 21 08:59:04 EST 2012,Tue Feb 21 08:59:24 EST 2012,312.4,
Tue Feb 21 08:59:24 EST 2012,Tue Feb 21 08:59:44 EST 2012,337.5,
Tue Feb 21 08:59:44 EST 2012,Tue Feb 21 09:00:04 EST 2012,379.45,
Tue Feb 21 09:00:04 EST 2012,Tue Feb 21 09:00:24 EST 2012,1182.75,
Tue Feb 21 09:00:24 EST 2012,Tue Feb 21 09:00:44 EST 2012,1193.75,
Tue Feb 21 09:00:44 EST 2012,Tue Feb 21 09:01:04 EST 2012,768.55,
Tue Feb 21 09:01:04 EST 2012,Tue Feb 21 09:01:24 EST 2012,700.55,
Tue Feb 21 09:01:24 EST 2012,Tue Feb 21 09:01:44 EST 2012,814.7,
Tue Feb 21 09:01:44 EST 2012,Tue Feb 21 09:02:04 EST 2012,682.9,
Tue Feb 21 09:02:04 EST 2012,Tue Feb 21 09:02:24 EST 2012,585.3,
Tue Feb 21 09:02:24 EST 2012,Tue Feb 21 09:03:04 EST 2012,506.55,
Tue Feb 21 09:03:04 EST 2012,Tue Feb 21 09:03:24 EST 2012,393.95,
Tue Feb 21 09:03:24 EST 2012,Tue Feb 21 09:03:44 EST 2012,940.1,
Tue Feb 21 09:03:44 EST 2012,Tue Feb 21 09:04:04 EST 2012,994.5,
Tue Feb 21 09:04:04 EST 2012,Tue Feb 21 09:04:24 EST 2012,792.65,
Tue Feb 21 16:56:24 EST 2012,Tue Feb 21 16:56:44 EST 2012,90.65,
Tue Feb 21 16:56:44 EST 2012,Tue Feb 21 16:57:04 EST 2012,110.5,
Tue Feb 21 16:57:04 EST 2012,Tue Feb 21 16:57:44 EST 2012,120.0,
Tue Feb 21 16:57:44 EST 2012,Tue Feb 21 16:58:04 EST 2012,97.3,
Tue Feb 21 16:58:04 EST 2012,Tue Feb 21 16:58:24 EST 2012,116.45,
Tue Feb 21 16:58:24 EST 2012,Tue Feb 21 16:58:44 EST 2012,107.25,
Tue Feb 21 16:58:44 EST 2012,Tue Feb 21 16:59:24 EST 2012,94.45,
Tue Feb 21 16:59:24 EST 2012,Tue Feb 21 16:59:44 EST 2012,115.3,
Tue Feb 21 16:59:44 EST 2012,Tue Feb 21 17:00:04 EST 2012,95.4,
Tue Feb 21 17:00:04 EST 2012,Tue Feb 21 17:00:24 EST 2012,236.4,
Tue Feb 21 17:00:24 EST 2012,Tue Feb 21 17:00:44 EST 2012,104.75,
Tue Feb 21 17:00:44 EST 2012,Tue Feb 21 17:01:24 EST 2012,114.15,
Tue Feb 21 17:01:24 EST 2012,Tue Feb 21 17:02:24 EST 2012,103.75,
Tue Feb 21 17:02:24 EST 2012,Tue Feb 21 17:03:04 EST 2012,91.45,
Tue Feb 21 17:03:04 EST 2012,Tue Feb 21 17:03:24 EST 2012,122.75,
Tue Feb 21 17:03:24 EST 2012,Tue Feb 21 17:03:44 EST 2012,115.95,
This is only a snippet at the 09:00:00 point and 17:00:00 point./ Theres just way to many lines to paste here. Also i need to remove the first section. As you can see it places the time and date twice. The first time and date is really void. im just owrried about the 2nd time and date.
Certainly. That's what makes it a 'one-liner'. Just enter that line at a commandline, or in a script. It prints the result to standard output, so redirection to a file will capture it for subsequent use.
You can also make it into a proper perl script, if you like:
Code:
#! /usr/bin/ perl -w
#
# I'll call this LQmikes88.pl
#
use strict;
while(<>){
my $tod=(split)[8];
my ($hr,$min,$sec)=split(/:/,$tod);
if( $hr>=9 && $hr<=16 ){
print $_;
}
}
Then, you would just do
Code:
./LQmikes88.pl <yourDataFile>
(after making it executable, with chmod +x)
It doesn't read as well as it might if it had been born as a full-on script. My one-liners tend to emphasize terseness.
druuna's solution is nice, elegant, and concise. I believe it can be reduced even further, by ignoring the minutes and seconds parts of the time field. It only needs to test the hours component:
druuna's solution is nice, elegant, and concise. I believe it can be reduced even further, by ignoring the minutes and seconds parts of the time field. It only needs to test the hours component:
Code:
awk '$9 ~ /(09|1[0-6])/ { print }' LQmikes88.dat
Hard to imagine anything smaller than that.
--- rod.
LOL, your end result is what I started with!
I do believe you need the extra bit. This time: 23:09:16 would otherwise be matched (twice), same would be true for 05:11:13.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.