ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a file I'm trying to figure out a way to output certain information from a log file. I'm looking for the regexp number and the network with the cidr (the /30 and /24).
Example of the log file:
input.txt
Router# show ip bgp regexp 65000
Network Next Hop Metric LocPrf Weight Path
* 10.1.0.0/30 172.16.0.6 0 300 100 ?
*> 172.16.0.1 0 0 100 ?
* 172.16.0.0/24 172.16.0.6 0 300 100 ?
* 172.16.0.1 0 0 100 ?
Router# show ip bgp regexp 65100
Network Next Hop Metric LocPrf Weight Path
* 10.2.0.0/30 172.16.0.6 0 300 100 ?
*> 172.16.0.1 0 0 100 ?
* 10.3.0.0/30 172.16.0.1 0 100 300 ?
* 172.17.0.0/30 172.16.0.6 0 300 100 ?
output.txt
65000
10.1.0.0/30
172.16.0.0/24
65100
10.2.0.0/30
10.3.0.0/30
172.17.0.0/30
I have: cat input.txt | egrep "/|show ip bgp regexp" | cut -d " " -f4 > output.txt but I'm getting:
These are all deprecated. Especially this cut can be handled by awk itself.
but in general avoid implementing cat|grep|awk|head|sed|wc|sort|... chains, usually a single awk (or similar) is enough.
KISS - for field based data I would usually also use awk, but if we take some assumptions based on request/limited data, grep can handle this in a readable manner.
( # opening
[ # character set beginning (bracket expression in man page)
[:digit:] # character class, digits: 0123456789
] # end of character set
+ # multiplier (one ore more)
$ # end of line
| # this is an OR
[ # again a char set
[:digit:] # including digits
. # and a dot
] # end of set
+ # multiplier
/ # just a /
[[:digit:]] # this could be [0-9] too
{1,2} # 1 or 2 instances allowed
) # closing
Thank you syg00 for the excellent one-line grep solution.
Thank you pan64 for the explanatory breakdown. Yes, man grep provides additional details but (for me) the complexity lies in the RegEx. I'm still digesting your post. One question: this ...
Code:
{1,2} # 1 or 2 instances allowed
... allows for 1 or 2 instances but the second "paragraph" in the InFile (headed 65100) has 3 instances of a slash, and yet the code handles them properly. (???)
I'm still digesting your post. One question: this ...
Code:
{1,2} # 1 or 2 instances allowed
... allows for 1 or 2 instances but the second "paragraph" in the InFile (headed 65100) has 3 instances of a slash, and yet the code handles them properly. (???)
{1,2} - repeater if you want to say - will work on the previous item only, which is in this case a character set or bracket expression [ ] (actually containing digits).
In short [[:digits:]]{1,2} means one or two digits
... allows for 1 or 2 instances but the second "paragraph" in the InFile (headed 65100) has 3 instances of a slash, and yet the code handles them properly. (???)
You are mixing CIDR network digit instances with CIDR instances. /[[:digit:]]{1,2} means that CIDR network value can be 1 or 2 digit-long (in our case, in the 2nd paragraph, the value is equal to "30" so 2 digit-long).
It's not related to the number of CIDR instances (3 instances which are: 10.2.0.0/30, 10.3.0.0/30 and 172.17.0.0/30 in the 2nd paragraph).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.