LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-16-2012, 05:02 PM   #16
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76

Hi.

Here is a grep solution -- not so different from awk, but the initial fields need to be skipped explicitly:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate extraction of time-of-day between intervals.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
edges() { local _f _n _l;: ${1?"edges: need file"}; _f=$1;_l=$(wc -l $_f);
  head -${_n:=10} $_f ; pe "--- ( $_l: lines total )" ; tail -$_n $_f ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C egrep

FILE=${1-data1}
pl " Sample of input file $FILE:"
head -14 $FILE

pl " Results, egrep:"
egrep '^(.* ){8}(09|1[0-6])' $FILE

exit 0
producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
egrep GNU grep 2.5.3

-----
 Sample of input file data1:
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,

-----
 Results, egrep:
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,
Best wishes ... cheers, makyo
 
Old 03-17-2012, 02:54 PM   #17
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
This gets a syntax error ...
Code:
awk '{if (8 < substr($9,1,2)) && (17 > substr($9,1,2)) {print $0}}'
... but I don't see why.

Daniel B. Martin
 
Old 03-18-2012, 01:44 AM   #18
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Quote:
Originally Posted by danielbmartin View Post
This gets a syntax error ...
Code:
awk '{if (8 < substr($9,1,2)) && (17 > substr($9,1,2)) {print $0}}'
... but I don't see why.

Daniel B. Martin
You should put parantheses around the argument of if
Code:
awk '{if ( (8 < substr($9,1,2)) && (17 > substr($9,1,2)) ) {print $0}}'
 
1 members found this post helpful.
Old 03-19-2012, 01:55 PM   #19
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by firstfire View Post
You should put parantheses around the argument of if
Code:
awk '{if ( (8 < substr($9,1,2)) && (17 > substr($9,1,2)) ) {print $0}}'
Good, thank you, another perfect answer from firstfire.

Daniel B. Martin
 
  


Reply



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] Grep -p for Linux, Trying to grep a paragraph. ohijames Linux - Newbie 5 07-22-2010 02:09 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
Data transfer online is slow "at times" or "stops at times" Balarabay1 SUSE / openSUSE 14 04-30-2006 10:00 AM
System hangs; Atheros Madwifi-ping times out every 15/16 times james 456 Linux - Networking 0 01-12-2006 06:55 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration