Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract lines in the middle 2

Status
Not open for further replies.

kHz

MIS
Dec 6, 2004
1,359
US
I have a text file that contains text like this (partial example):

12:38am 0.72
12:39am 0.60
12:40am 0.57
12:41am 0.53
12:42am 0.50
12:43am 0.65
12:44am 0.57
12:45am 0.67
12:46am 0.60
12:47am 0.58
12:48am 0.63
12:49am 0.53
12:50am 0.58
12:51am 0.59
12:52am 0.52
12:53am 0.57
12:54am 0.54
12:55am 0.53
12:56am 0.57
12:57am 0.54
12:58am 0.53
12:59am 0.68
1:00am 0.45
1:01am 0.67
1:02am 0.46
1:03am 0.46
1:04am 0.43
1:05am 0.37
1:06am 0.47
1:07am 0.43
1:08am 0.40
1:09am 0.41
1:10am 0.52
1:11am 0.38
1:12am 0.36
1:13am 0.37
1:15am 0.34

The collection begins at 12:00am and ends at 23:59pm. I need to extract only the lines between 08:00am and 16:59pm and save them to another file.
 
Your data sample skips from 1:13am to 1:15am.
In order for this to work, "8:00am" and "15:59pm" must be in the data.
Code:
awk '"8:00am"==$1,"16:59pm"==$1' oldfile >newfile
By the way, "16:59pm" is redundant. "16:59" would suffice.
 
Hi

Yes, but look at the sample. There is no 1:1[red]4[/red]am. So may be gaps. And if 8:00am or 16:59pm is missing, is impossible to use a text based solution. In this case must calculate seconds since midnight :
Code:
awk '
function sec()
{
  match($0,"([[:digit:]]+):([[:digit:]]+)([ap])m",a)
  return a[1]%12*60+a[2]+(a[3]=="p"?12*60:0)
}
sec()>=8*60 && sec()<17*60
' oldfile

Feherke.
 
futurelet said:
Your data sample skips from 1:13am to 1:15am.
feherke said:
look at the sample. There is no 1:14am.
futurelet said:
In order for this to work, "8:00am" and "15:59pm" must be in the data.
feherke said:
And if 8:00am or 16:59pm is missing, is impossible to use a text based solution.
What are you doing, feherke?
Code:
function time(s)
{ return s*60 + substr(s,index(s,":")+1) }
time($1)>=time("8:00")&&time($1)<=time("16:59")
 
Hi

I am really sorry, futurelet. I missed somehow your first paragraph. :-( I think I should not post here before coffee.

Beside this, the 12am in the sample seems to be midnight instead of noon, otherwise should be followed by 13 or 1pm. This is why my function is abit more complicated.

Feherke.
 
Don't worry about it, feherke. I'm sure it was an honest mistake.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top