Here is a commented version of the script I suggested:
#!/bin/ksh
# Set IAm to the final component of the pathname used to invoke this script.
IAm=${0##*/}
# If the number of operands given to this script is not 3, print a usage
# message to the standard error output and exit.
if [ $# -ne 3 ]
then printf 'Usage: %s year.month1 year.month2 filename\n' "$IAm" >&2
exit 1
fi
# Use awk to print any line in the file whose pathname is given by the 3rd
# operand to this script ("$3") where the field separator for lines in the file
# is set to a <period> character (-F'.') and:
# 1. the input line number is 1 (NR==1): to print the header, or (||)
# 2. the 1st field followed by the field separator followed by the 2nd field
# is the string specified by the 1st operand to this script (-v ym1="$1")
# and ($1 FS $2==ym1): to print lines matching the 1st given year
# and month, or (||)
# 3. the 1st field followed by the field separator followed by the 2nd field
# is the string specified by the 2nd operand to this script (-v ym2="$2")
# and ($1 FS $2==ym2): to print lines matching the 2nd given year
# and month.
awk -F'.' -v ym1="$1" -v ym2="$2" 'NR==1 || $1 FS $2==ym1 || $1 FS $2==ym2' "$3"
Is this enough explanation, or is there something that still isn't clear?
If the date you want to process is in the 12th field and the field separator is a <tab> character and there is no header line (or the header line does not appear as the 1st line and doesn't need to be copied to the output) as in your latest sample input, you could try the following script instead:
#!/bin/ksh
# Set IAm to the final component of the pathname used to invoke this script.
IAm=${0##*/}
# If the number of operands given to this script is not 3, print a usage
# message and exit.
if [ $# -ne 3 ]
then printf 'Usage: %s year.month1 year.month2 filename\n' "$IAm" >&2
exit 1
fi
awk -F'\t' -v ym1="$1" -v ym2="$2" '
#NR == 1 {
# The following code to print the header has been removed because the sample
# data does not have a header line.
# # Print the header line and skip further processing for this line.
# print
# next
#}
{ # Split the 12th tab separated field into the ymd[] array using
# <period> as the field # separator within this field.
split($12, ymd, /[.]/)
#printf("$12=%s, ymd[1]=%s, ymd[2]=%s\n", $12, ymd[1], ymd[2])
# Print this line if ym1 or ym2 is the same string as ymd[1] "." ymd[2].
if(ym1 == ymd[1] "." ymd[2] || ym2 == ymd[1] "." ymd[2])
print
}' "$3"
If someone wants to try either of the above scripts on a Solaris/SunOS system, change awk
to /usr/xpg4/bin/awk
or nawk
.
As has been said before, giving us data that is not at all representative of the data you want to process, not telling us what the field separator is in the data you want to process, and continually changing your requirements is a huge waste of everyone's time.
I hope the above helps you, but if this still doesn't meet your requirements, we have little incentive to try to help if your requirements change again.