Regex awk to Cisco Interface












-1














I would like to know if have some Sed/Grep or Awk regex to parse Cisco interface section, with specific attribute, like bellow.



Content of file.txt



!
interface FastEthernet0/1
no ip unreachables
!
interface FastEthernet0/2
no ip proxy-arp
!


Script:



#!/bin/bash
VALUE="no ip proxy-arp"
awk -v RS='!n' -v PATTERN=${VALUE} '/$PATTERN/' file.txt | awk '/^interface/';
exit 0


The problem is when I run line directly from shell, it work, but when I run from script, it don't work.



Running with bash -x, I can see that awk can't replace variable value.



Any suggestions ?










share|improve this question
























  • Something like grep -B1 'no ip proxy-arp' file.txt | head -1? What is the expected output?
    – Teresa e Junior
    Apr 12 '13 at 3:16












  • @TeresaeJunior I don't know whether this is necessary but the awk solutions deliver (possibly) more than one line. Thus the second part of the pipeline should be e.g. another grep.
    – Hauke Laging
    Apr 12 '13 at 4:07






  • 1




    FYI, you asked for sed|grep|awk assistance; however, ciscoconfparse is a python module which specializes in handling these kind of tasks. If you do a lot of this, it may be worth your while to consider using a library which is built for parsing cisco configs.
    – Mike Pennington
    Feb 8 '14 at 19:31
















-1














I would like to know if have some Sed/Grep or Awk regex to parse Cisco interface section, with specific attribute, like bellow.



Content of file.txt



!
interface FastEthernet0/1
no ip unreachables
!
interface FastEthernet0/2
no ip proxy-arp
!


Script:



#!/bin/bash
VALUE="no ip proxy-arp"
awk -v RS='!n' -v PATTERN=${VALUE} '/$PATTERN/' file.txt | awk '/^interface/';
exit 0


The problem is when I run line directly from shell, it work, but when I run from script, it don't work.



Running with bash -x, I can see that awk can't replace variable value.



Any suggestions ?










share|improve this question
























  • Something like grep -B1 'no ip proxy-arp' file.txt | head -1? What is the expected output?
    – Teresa e Junior
    Apr 12 '13 at 3:16












  • @TeresaeJunior I don't know whether this is necessary but the awk solutions deliver (possibly) more than one line. Thus the second part of the pipeline should be e.g. another grep.
    – Hauke Laging
    Apr 12 '13 at 4:07






  • 1




    FYI, you asked for sed|grep|awk assistance; however, ciscoconfparse is a python module which specializes in handling these kind of tasks. If you do a lot of this, it may be worth your while to consider using a library which is built for parsing cisco configs.
    – Mike Pennington
    Feb 8 '14 at 19:31














-1












-1








-1







I would like to know if have some Sed/Grep or Awk regex to parse Cisco interface section, with specific attribute, like bellow.



Content of file.txt



!
interface FastEthernet0/1
no ip unreachables
!
interface FastEthernet0/2
no ip proxy-arp
!


Script:



#!/bin/bash
VALUE="no ip proxy-arp"
awk -v RS='!n' -v PATTERN=${VALUE} '/$PATTERN/' file.txt | awk '/^interface/';
exit 0


The problem is when I run line directly from shell, it work, but when I run from script, it don't work.



Running with bash -x, I can see that awk can't replace variable value.



Any suggestions ?










share|improve this question















I would like to know if have some Sed/Grep or Awk regex to parse Cisco interface section, with specific attribute, like bellow.



Content of file.txt



!
interface FastEthernet0/1
no ip unreachables
!
interface FastEthernet0/2
no ip proxy-arp
!


Script:



#!/bin/bash
VALUE="no ip proxy-arp"
awk -v RS='!n' -v PATTERN=${VALUE} '/$PATTERN/' file.txt | awk '/^interface/';
exit 0


The problem is when I run line directly from shell, it work, but when I run from script, it don't work.



Running with bash -x, I can see that awk can't replace variable value.



Any suggestions ?







awk regular-expression cisco interface






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 20 '13 at 7:15









Anthon

60.2k17102163




60.2k17102163










asked Apr 11 '13 at 17:47









Robert

12




12












  • Something like grep -B1 'no ip proxy-arp' file.txt | head -1? What is the expected output?
    – Teresa e Junior
    Apr 12 '13 at 3:16












  • @TeresaeJunior I don't know whether this is necessary but the awk solutions deliver (possibly) more than one line. Thus the second part of the pipeline should be e.g. another grep.
    – Hauke Laging
    Apr 12 '13 at 4:07






  • 1




    FYI, you asked for sed|grep|awk assistance; however, ciscoconfparse is a python module which specializes in handling these kind of tasks. If you do a lot of this, it may be worth your while to consider using a library which is built for parsing cisco configs.
    – Mike Pennington
    Feb 8 '14 at 19:31


















  • Something like grep -B1 'no ip proxy-arp' file.txt | head -1? What is the expected output?
    – Teresa e Junior
    Apr 12 '13 at 3:16












  • @TeresaeJunior I don't know whether this is necessary but the awk solutions deliver (possibly) more than one line. Thus the second part of the pipeline should be e.g. another grep.
    – Hauke Laging
    Apr 12 '13 at 4:07






  • 1




    FYI, you asked for sed|grep|awk assistance; however, ciscoconfparse is a python module which specializes in handling these kind of tasks. If you do a lot of this, it may be worth your while to consider using a library which is built for parsing cisco configs.
    – Mike Pennington
    Feb 8 '14 at 19:31
















Something like grep -B1 'no ip proxy-arp' file.txt | head -1? What is the expected output?
– Teresa e Junior
Apr 12 '13 at 3:16






Something like grep -B1 'no ip proxy-arp' file.txt | head -1? What is the expected output?
– Teresa e Junior
Apr 12 '13 at 3:16














@TeresaeJunior I don't know whether this is necessary but the awk solutions deliver (possibly) more than one line. Thus the second part of the pipeline should be e.g. another grep.
– Hauke Laging
Apr 12 '13 at 4:07




@TeresaeJunior I don't know whether this is necessary but the awk solutions deliver (possibly) more than one line. Thus the second part of the pipeline should be e.g. another grep.
– Hauke Laging
Apr 12 '13 at 4:07




1




1




FYI, you asked for sed|grep|awk assistance; however, ciscoconfparse is a python module which specializes in handling these kind of tasks. If you do a lot of this, it may be worth your while to consider using a library which is built for parsing cisco configs.
– Mike Pennington
Feb 8 '14 at 19:31




FYI, you asked for sed|grep|awk assistance; however, ciscoconfparse is a python module which specializes in handling these kind of tasks. If you do a lot of this, it may be worth your while to consider using a library which is built for parsing cisco configs.
– Mike Pennington
Feb 8 '14 at 19:31










2 Answers
2






active

oldest

votes


















3














Hard to believe that this works in your shell. Nonetheless this code contains several errors and also has the wrong approach IMHO.



awk expects a string between // not a variable. These are constant regular expressions. So you either make the shell put the variable there or you use ~.



Your approach with corrections:



awk -v RS='!n' -v PATTERN="${VALUE}" '$0 ~ PATTERN' file.txt | 
awk '/^interface/'


I am surprised that this works. From the documentation I had expected that due to the setting of RS an unwanted "!" would be printed. However, I consider this one better:



awk -v PATTERN="${VALUE}" 
'$0 ~ PATTERN { print previousline; }; { previousline=$0; }' file.txt


or with hardcoded pattern



awk '/no ip proxy-arp/ { print previousline; }; { previousline=$0; }' file.txt


or with the shell writing the pattern



awk /"$VALUE"/' { print previousline; }; { previousline=$0; }' file.txt





share|improve this answer























  • +1 for previousline. Nice one :)
    – Teresa e Junior
    Apr 12 '13 at 5:11



















-1















echo "!
interface FastEthernet0/1
no ip unreachables
!
interface FastEthernet0/2
no ip proxy-arp
" | pcregrep -M "^interface .*0/1n (.*n)!"


result:




interface FastEthernet0/1
no ip unreachables





share|improve this answer





















  • The OP only wants to show interfaces containing the text no ip proxy-arp, and your command doesn't check for that. Your result only shows one section (the wrong one) because you've omitted the ! from the end of the input.
    – JigglyNaga
    Dec 10 at 10:31











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f72106%2fregex-awk-to-cisco-interface%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Hard to believe that this works in your shell. Nonetheless this code contains several errors and also has the wrong approach IMHO.



awk expects a string between // not a variable. These are constant regular expressions. So you either make the shell put the variable there or you use ~.



Your approach with corrections:



awk -v RS='!n' -v PATTERN="${VALUE}" '$0 ~ PATTERN' file.txt | 
awk '/^interface/'


I am surprised that this works. From the documentation I had expected that due to the setting of RS an unwanted "!" would be printed. However, I consider this one better:



awk -v PATTERN="${VALUE}" 
'$0 ~ PATTERN { print previousline; }; { previousline=$0; }' file.txt


or with hardcoded pattern



awk '/no ip proxy-arp/ { print previousline; }; { previousline=$0; }' file.txt


or with the shell writing the pattern



awk /"$VALUE"/' { print previousline; }; { previousline=$0; }' file.txt





share|improve this answer























  • +1 for previousline. Nice one :)
    – Teresa e Junior
    Apr 12 '13 at 5:11
















3














Hard to believe that this works in your shell. Nonetheless this code contains several errors and also has the wrong approach IMHO.



awk expects a string between // not a variable. These are constant regular expressions. So you either make the shell put the variable there or you use ~.



Your approach with corrections:



awk -v RS='!n' -v PATTERN="${VALUE}" '$0 ~ PATTERN' file.txt | 
awk '/^interface/'


I am surprised that this works. From the documentation I had expected that due to the setting of RS an unwanted "!" would be printed. However, I consider this one better:



awk -v PATTERN="${VALUE}" 
'$0 ~ PATTERN { print previousline; }; { previousline=$0; }' file.txt


or with hardcoded pattern



awk '/no ip proxy-arp/ { print previousline; }; { previousline=$0; }' file.txt


or with the shell writing the pattern



awk /"$VALUE"/' { print previousline; }; { previousline=$0; }' file.txt





share|improve this answer























  • +1 for previousline. Nice one :)
    – Teresa e Junior
    Apr 12 '13 at 5:11














3












3








3






Hard to believe that this works in your shell. Nonetheless this code contains several errors and also has the wrong approach IMHO.



awk expects a string between // not a variable. These are constant regular expressions. So you either make the shell put the variable there or you use ~.



Your approach with corrections:



awk -v RS='!n' -v PATTERN="${VALUE}" '$0 ~ PATTERN' file.txt | 
awk '/^interface/'


I am surprised that this works. From the documentation I had expected that due to the setting of RS an unwanted "!" would be printed. However, I consider this one better:



awk -v PATTERN="${VALUE}" 
'$0 ~ PATTERN { print previousline; }; { previousline=$0; }' file.txt


or with hardcoded pattern



awk '/no ip proxy-arp/ { print previousline; }; { previousline=$0; }' file.txt


or with the shell writing the pattern



awk /"$VALUE"/' { print previousline; }; { previousline=$0; }' file.txt





share|improve this answer














Hard to believe that this works in your shell. Nonetheless this code contains several errors and also has the wrong approach IMHO.



awk expects a string between // not a variable. These are constant regular expressions. So you either make the shell put the variable there or you use ~.



Your approach with corrections:



awk -v RS='!n' -v PATTERN="${VALUE}" '$0 ~ PATTERN' file.txt | 
awk '/^interface/'


I am surprised that this works. From the documentation I had expected that due to the setting of RS an unwanted "!" would be printed. However, I consider this one better:



awk -v PATTERN="${VALUE}" 
'$0 ~ PATTERN { print previousline; }; { previousline=$0; }' file.txt


or with hardcoded pattern



awk '/no ip proxy-arp/ { print previousline; }; { previousline=$0; }' file.txt


or with the shell writing the pattern



awk /"$VALUE"/' { print previousline; }; { previousline=$0; }' file.txt






share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 12 '13 at 18:54

























answered Apr 12 '13 at 3:57









Hauke Laging

55.6k1285133




55.6k1285133












  • +1 for previousline. Nice one :)
    – Teresa e Junior
    Apr 12 '13 at 5:11


















  • +1 for previousline. Nice one :)
    – Teresa e Junior
    Apr 12 '13 at 5:11
















+1 for previousline. Nice one :)
– Teresa e Junior
Apr 12 '13 at 5:11




+1 for previousline. Nice one :)
– Teresa e Junior
Apr 12 '13 at 5:11













-1















echo "!
interface FastEthernet0/1
no ip unreachables
!
interface FastEthernet0/2
no ip proxy-arp
" | pcregrep -M "^interface .*0/1n (.*n)!"


result:




interface FastEthernet0/1
no ip unreachables





share|improve this answer





















  • The OP only wants to show interfaces containing the text no ip proxy-arp, and your command doesn't check for that. Your result only shows one section (the wrong one) because you've omitted the ! from the end of the input.
    – JigglyNaga
    Dec 10 at 10:31
















-1















echo "!
interface FastEthernet0/1
no ip unreachables
!
interface FastEthernet0/2
no ip proxy-arp
" | pcregrep -M "^interface .*0/1n (.*n)!"


result:




interface FastEthernet0/1
no ip unreachables





share|improve this answer





















  • The OP only wants to show interfaces containing the text no ip proxy-arp, and your command doesn't check for that. Your result only shows one section (the wrong one) because you've omitted the ! from the end of the input.
    – JigglyNaga
    Dec 10 at 10:31














-1












-1








-1







echo "!
interface FastEthernet0/1
no ip unreachables
!
interface FastEthernet0/2
no ip proxy-arp
" | pcregrep -M "^interface .*0/1n (.*n)!"


result:




interface FastEthernet0/1
no ip unreachables





share|improve this answer













echo "!
interface FastEthernet0/1
no ip unreachables
!
interface FastEthernet0/2
no ip proxy-arp
" | pcregrep -M "^interface .*0/1n (.*n)!"


result:




interface FastEthernet0/1
no ip unreachables






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 9 at 1:20









Mac Camel

1




1












  • The OP only wants to show interfaces containing the text no ip proxy-arp, and your command doesn't check for that. Your result only shows one section (the wrong one) because you've omitted the ! from the end of the input.
    – JigglyNaga
    Dec 10 at 10:31


















  • The OP only wants to show interfaces containing the text no ip proxy-arp, and your command doesn't check for that. Your result only shows one section (the wrong one) because you've omitted the ! from the end of the input.
    – JigglyNaga
    Dec 10 at 10:31
















The OP only wants to show interfaces containing the text no ip proxy-arp, and your command doesn't check for that. Your result only shows one section (the wrong one) because you've omitted the ! from the end of the input.
– JigglyNaga
Dec 10 at 10:31




The OP only wants to show interfaces containing the text no ip proxy-arp, and your command doesn't check for that. Your result only shows one section (the wrong one) because you've omitted the ! from the end of the input.
– JigglyNaga
Dec 10 at 10:31


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f72106%2fregex-awk-to-cisco-interface%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre