Solved AWK line print query

June 20, 2013 at 16:07:02
Specs: AIX 5.3, 4 CPU / 32 GB RAM
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 :


See More: AWK line print query

Report •

#1
June 22, 2013 at 16:13:24
✔ Best Answer
#!/bin/ksh

# developed on Solaris 9 using nawk
##ddate="01/01/2013"
##ddate="04/01/2013"
ddate="10/01/2013"
awk -v dd=$ddate ' BEGIN { FS=":"; f_flag=0; h_flag=0; }
{
if(dd == $1)
   { # find which line contains the date
     # and set the found flag
   f_flag=1
   save_heading=$0
   continue
   }
if(f_flag)
   {
   if(!h_flag)
      { # print the heading only once
      print "Select="dd
      print save_heading
      h_flag=1
      }

   if($2 == "Sat " || $2 == "Sun ")
      continue # skip any weekend line

   if($3 == "XXX")
      continue # skip any line where the 3rd field is XXX

   print $0 # print out the desired line
   exit; # and quit
   }

} ' inputfile.txt


Report •

#2
June 23, 2013 at 09:37:05
Thank you nails - an excellent solution that did the trick.

Report •
Related Solutions


Ask Question