Showing results for 
Search instead for 
Do you mean 

AWK line grab

SOLVED
Go to Solution
Highlighted
Occasional Contributor

AWK line grab

Hello,

I'm looking for an AWK solution for the following.
Using the below input file I would like to select a date (Mon-Fri) and then:

1. Print the current line
2. Print the next week day line (i.e. Mon-Fri) however if that day is a "XXX" line it must be the next day

Thanks in advance for any solutions.

Input file

01/01/2013:Tue :
02/01/2013:Wed :
03/01/2013:Thu :XXX
04/01/2013:Fri :
05/01/2013:Sat :
06/01/2013:Sun :
07/01/2013:Mon :
08/01/2013:Tue :
09/01/2013:Wed :
10/01/2013:Thu :
11/01/2013:Fri :XXX
12/01/2013:Sat :
13/01/2013:Sun :
14/01/2013:Mon :XXX
15/01/2013:Tue :
16/01/2013:Wed :
17/01/2013:Thu :
18/01/2013:Fri :
19/01/2013:Sat :


Sample desired output for 3 sample runs
Run #1
Select=01/01/2013
01/01/2013:Tue :
02/01/2013:Wed :

Run #2
Select=04/01/2013
04/01/2013:Fri :
07/01/2013:Mon :

Run #3
select=10/01/2013
10/01/2013:Thu :
15/01/2013:Tue :

1 ACCEPTED SOLUTIONS
Acclaimed Contributor Acclaimed Contributor

Re: awk line grab

[ Edited ]

You've neglected to assign kudos and mark a solution to your previous awk query:

http://h30499.www3.hp.com/t5/Languages-and-Scripting/Run-awk-script-from-within-KSH-script/m-p/54219...

 

>I'm looking for an awk solution for the following.

 

Perhaps something like the following, where $select is your date.

awk -F: -v select=$select '
BEGIN { found_it = 0 }
# find next week day after select, skipping XXX days
found_it {
   if ($2 == "Sat " || $2 == "Sun ") next
   if ($3 == "XXX") next
   print $0
   exit
}
$1 == select {
   found_it = 1
   print $0
   next
}' input-file

3 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: awk line grab

[ Edited ]

You've neglected to assign kudos and mark a solution to your previous awk query:

http://h30499.www3.hp.com/t5/Languages-and-Scripting/Run-awk-script-from-within-KSH-script/m-p/54219...

 

>I'm looking for an awk solution for the following.

 

Perhaps something like the following, where $select is your date.

awk -F: -v select=$select '
BEGIN { found_it = 0 }
# find next week day after select, skipping XXX days
found_it {
   if ($2 == "Sat " || $2 == "Sun ") next
   if ($3 == "XXX") next
   print $0
   exit
}
$1 == select {
   found_it = 1
   print $0
   next
}' input-file

Occasional Contributor

Re: awk line grab

Thank you Dennis - a great reply.

 

I take it the 'fount_it' block is the equivalent of saying:

 

if(found_it==1)

 

... and could follow the '$1 == select' block of code?

 

Thanks again.

 

 

 

 

Acclaimed Contributor Acclaimed Contributor

Re: awk line grab

[ Edited ]

>the found_it block is the equivalent of saying:  if (found_it==1)

 

More accurately: found_it != 0.

 

>could follow the "$1 == select" block of code?

 

Provided you don't have duplicate dates.

 

Also, it would be helpful to others to mark the the solution post with the Accept as Solution button.