Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Showing results for 
Search instead for 
Did you mean: 

AWK line grab

Go to solution
Occasional Contributor

AWK line grab


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
01/01/2013:Tue :
02/01/2013:Wed :

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

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

Dennis Handly
Acclaimed Contributor

Re: awk line grab

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


>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
$1 == select {
   found_it = 1
   print $0
}' 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:




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


Thanks again.





Dennis Handly
Acclaimed Contributor

Re: awk line grab

>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.