Reputation: 1702
the following awk syntax cut the lines from the file
from the line that have port XNT1
until END OF COMMAND line
# awk '/\/stats\/port XNT1\/if/,/END OF COMMAND/' /var/tmp/test
>> SW_02_03 - Main# /stats/port XNT1/if
------------------------------------------------------------------
Interface statistics for port XNT1:
IBP/CBP Discards: 0
L3 Discards: 0
>> SW_02_03 - Port Statistics# END OF COMMAND
#
#
#
now I set external variable as XNTF=XNT1 in awk command
but from some reason XNTF in the awk not get the "XNT1" value , and awk not display the lines!!!!!!!!
# awk -v XNTF=XNT1 '/\/stats\/port XNTF\/if/,/END OF COMMAND/' /var/tmp/test
please advice why awk not works when I set external variable ? and how to fix it ?
Upvotes: 0
Views: 122
Reputation: 41460
I normally try to avoid the range command in awk
,
, since it not so flexible. This should do:
awk -v XNTF=XNT1 '$0~"/stats/port " XNTF "/if" {f=1} f; /END OF COMMAND/ {f=0}' file
>> SW_02_03 - Main# /stats/port XNT1/if
------------------------------------------------------------------
Interface statistics for port XNT1:
IBP/CBP Discards: 0
L3 Discards: 0
>> SW_02_03 - Port Statistics# END OF COMMAND
Upvotes: 2
Reputation: 44063
Inside //
, variables are not expanded. You'll have to use the ~
operator to match against an assembled regex:
awk -v XNTF=XNT1 '$0 ~ "/stats/port " XNTF "/if",/END OF COMMAND/' /var/tmp/test
Generally, $0 ~ some_string
matches $0
(the line) against some_string
interpreted as a regex.
Upvotes: 1