If you don't like perl
, or if you want a commented script to help you understand what is going on, you could also consider the following bash
and /usr/xpg4/bin/awk
scripts that should work fine on a Solaris 10 system:
#!/bin/bash
# Extract script name from final component of path used to invoke this script.
IAm=${0##*/}
# Verify that we have been called with two operands...
if [ $# -ne 2 ]
then # We were not called with two operands, print usage diagnostic and exit.
printf 'Usage: %s input_filename output_filename\n' "$IAm" >&2
exit 1
fi
# The 1st operand (input_filename) is assumed to contain multi-line records
# with each record containing a line of the form (without the leading "# "):
# dn: EpsStaInfId=EpsStaInf,serv=EPS,mscId=string1,ou=multiSCs,dc=mtncg
# followed by a later line in the record of the form (again without the
# leadin "# ":
# EpsProfileId: string2
# where "string1" in the 1st line is a 32 character fixed-length alphanumeric
# string. If the 6 characters starting at position 18 in this string are
# "629100" as in "aaaaaa001aaaaaaaa629100100138702", we need to extract and
# print the 3rd comma separated field from the 1st line and the entire 2nd line
# and print them as comma separated fields in a single output line in the file
# named by the 2nd operand (output_filename). If those 6 characters are any
# other string, nothgin from that record from input_filename will be included
# in output_filename.
# Use awk with input fields separated by colons and commas and the output field
# separator set to comma to perform the above specified actions.
/usr/xpg4/bin/awk -F '[:,]' -v OFS=, '
# If the 1st field on the line is the string "dn", determine whether or not
# this is an unencrypted record.
$1 == "dn" {
# Uncomment the next line to see the fields we are examining.
#printf("*$1=\"%s\" $4=\"%s\" substr($4,24,6)=\"%s\"\n", $1, $4, substr($4, 24, 6))
# If the string "629100" appears starting at position 24 (note that we
# have "mscId=string1" here, not just "string1"):
if(substr($4, 24, 6) == "629100") {
# it is, so we have an unencrypted record.
unencrypted = 1
# save the mscId field from this line to be printed later.
mscId = $4
} else {# t is not, so we have an encrypted record.
unencrypted = 0
}
# We are done with this record, so move on to the next input line.
next
}
# If we are processing an unencrypted record, look for the "EpsProfileId" data.
unencrypted && ($1 == "EpsProfileId") {
# We found it. Print the saved mscId from the previous "dn:" line and
# the "EpsProfileId" data from this line.
print mscId, $0
# and clear the unencrypted flag to speed up processing until we find
# the next "dn:" line.
unencrypted = 0
}' "$1" > "$2" # End the awk script naming the file to be proccessed and
# redirecting output to the specified output file.
or an alternative awk
approach (without the comments):
#!/bin/bash
# Extract script name from final component of path used to invoke this script.
IAm=${0##*/}
# Verify that we have been called with two operands...
if [ $# -ne 2 ]
then # We were not called with two operands, print usage diagnostic and exit.
printf 'Usage: %s input_filename output_filename\n' "$IAm" >&2
exit 1
fi
/usr/xpg4/bin/awk -F '[:,]' -v OFS=, '
$1 == "dn" { m = (substr($4, 24, 6) == "629100")? $4 : 0; next }
m && ($1 == "EpsProfileId") { print m, $0; m = 0 }' "$1" > "$2"
both of which produce the output you said you wanted in post #12.
In both of these nawk
should work as well as /usr/xpg4/bin/awk
.
If you uncomment the debugging printf
command in the 1st script, you'll get the output:
*$1="dn" $4="mscId=aaa8c4c6b30a4b1abcfce3990027d6a4" substr($4,24,6)="cfce39"
*$1="dn" $4="mscId=aaa9526a822d46979331a964be201cba" substr($4,24,6)="331a96"
*$1="dn" $4="mscId=aaaaaa001aaaaaaaa629100100138702" substr($4,24,6)="629100"
mscId=aaaaaa001aaaaaaaa629100100138702,EpsProfileId: 29
*$1="dn" $4="mscId=aaaaaa001aaaaaaaa629100100165619" substr($4,24,6)="629100"
mscId=aaaaaa001aaaaaaaa629100100165619,EpsProfileId: 29
showing how awk
split the fields in the dn:
lines.