Quote:
Originally Posted by CyberIT
I couldnt get it to work and for me got a little confusing...
Thanks!
|
By the way, please show at least a few lines of sample input and the corresponding desired output. The one line you have does not look quite like a SRV record though it does appear similar.
As for #10 above, could you be more specific about what you tried?
Here is a slightly modified version and a breakdown piece by piece:
Code:
record="SRV"
a=($(echo _mon._tcp.tax.ex.example.com has SRV record 0 5 3000 mdb-nkj.ex2.example.com. \
| awk -vr="$record" '$0~r {l=split($NF,a,/\./); print a[l-4],a[l-3]}'))
echo Array element 0 = ${a[0]}
echo Array element 1 = ${a[1]}
The example is shell scripting, Bash to be specific.
In Bash,
a=(...) means make an array. See:
Code:
a=("foo" "bar")
echo ${a}
echo ${a[0]}
echo ${a[1]}
And
$(...) is command substitution. That is where what's between the parentheses is evaluated and used as input. Compare:
Code:
echo "Right now it is date +'%F %T'"
echo "Right now it is $(date +'%F %T')"
The AWK script works if you set the environment variable "$record" in the shell first.
-vr="$record" makes the shell variable $record available within the AWK script as the internal variable "r".
$0~r {...} means search each line for what's in the variable "r" and then executes what's between the braces
if it is found. The "$0" stands for the whole line or record. The tilde ~ is the pattern search and the contents of the variable "r" is what is searched for.
l=split($NF,a,/\./);
The variables themselves have no dollar sign in front, and adding a dollar sign changes the meaning. This is fundamental to AWK. See the reference manual for the AWK scripting language, "man awk", or
Bruce Barnett's AWK grymoire before proceeding.
AWK has some built-in variables. One built-in variable is NF which holds the number of fields in the current record being examined. In other words, NF contains the number of the last field. Thus $NF is the content of the last field, just like $1 is the conent of the first field, and $2 is the content of the second field and so on. So the split() function divides the content "$NF" of that last field , which should be "mdb-nkj.ex.example.com." in your example, using a dot \. as the separator. The results are stored as an array in the variable "a" and the size of that resulting array is stored in the variable "l". In other words "a[l]" would point to the last element in the array "a" and "a[0]" would indicate the first element in the array "a".
print a[l-4],a[l-3]}')
Then since the variable "l" now contains the number of the last element in the array, the fourth-from-last and third-from-last array elements are printed, separated by the Output Field Separator. The default for that is one space. That output field separator can be changed by setting the OFS variable in AWK.
However, all that might be a lot more than what you need or it might just be very different than what you need. So, again, please show at least a few more lines of sample input and the corresponding desired output.