LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2018, 10:25 AM   #1
blah1234
LQ Newbie
 
Registered: Dec 2018
Posts: 1

Rep: Reputation: Disabled
grep | cut help


Hello,

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:

regexp
10.1.0.0/30
172.16.0.0/30
regexp
10.2.0.0/30
10.3.0.0/30
172.17.0.0/30


Any suggestions on using cut with multiple delimiters? I'm open to another method of doing this. Thanks in advanced!

Last edited by blah1234; 12-18-2018 at 10:33 AM.
 
Old 12-18-2018, 10:36 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,791

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
this is a job for awk:
Code:
awk '/pattern/ { print something }
     /pattern/ { print something }
 .... '
 
1 members found this post helpful.
Old 12-18-2018, 10:54 AM   #3
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Code:
awk '/Router# show ip bgp regexp/{print $6} $2 ~ /\//{print $2}' input.txt
 
2 members found this post helpful.
Old 12-18-2018, 12:57 PM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,134
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Any one of these examples, and more.
Code:
cut -d ' ' -f6 <<< $(grep 'Router' input.txt)

grep 'Router' input.txt | cut -d ' ' -f6

awk '/Router/' input.txt | cut -d ' '  -f6

awk '/Router/{print $6}' input.txt

head -n1 input.txt | cut -d ' '  -f6

grep -o '[0-9][0-9][0-9][0-9][0-9]' input.txt

grep  -o '[0-9]\{5\}' input.txt
 
Old 12-18-2018, 02:47 PM   #5
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
^ Global remark: you've missed the CIDR requirement part
Code:
awk '/Router/' input.txt | cut -d ' '  -f6
Really not optimized
Code:
grep -o '[0-9][0-9][0-9][0-9][0-9]' input.txt
grep  -o '[0-9]\{5\}' input.txt
Very hazardous as OP didn't say numbers after "regexp" are always 5 digits long
 
Old 12-19-2018, 03:04 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,791

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
Quote:
Originally Posted by teckk View Post
Any one of these examples, and more.
Code:
cut -d ' ' -f6 <<< $(grep 'Router' input.txt)

grep 'Router' input.txt | cut -d ' ' -f6

awk '/Router/' input.txt | cut -d ' '  -f6
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.
 
1 members found this post helpful.
Old 12-19-2018, 03:32 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
Code:
grep -oE "([[:digit:]]+$|[[:digit:].]+/[[:digit:]]{1,2})" input.file
 
3 members found this post helpful.
Old 12-19-2018, 06:37 AM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by syg00 View Post
... grep can handle this in a readable manner.
Code:
grep -oE "([[:digit:]]+$|[[:digit:].]+/[[:digit:]]{1,2})" input.file
An impressive solution! Impressive, but not readable for those with lesser skills (such as me). Please explain!

Daniel B. Martin

.
 
Old 12-19-2018, 07:43 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,791

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
it is quite easy, if you find a way to split it:
Code:
(                # 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
see man grep for additional details
 
3 members found this post helpful.
Old 12-19-2018, 10:53 AM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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. (???)

Daniel B. Martin

.
 
Old 12-19-2018, 11:03 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,791

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
Quote:
Originally Posted by danielbmartin View Post
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
 
1 members found this post helpful.
Old 12-19-2018, 11:06 AM   #12
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by danielbmartin View 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. (???)
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).

Last edited by l0f4r0; 12-19-2018 at 11:10 AM.
 
1 members found this post helpful.
Old 12-19-2018, 06:20 PM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Thanks from me too to pan64 for the excellent breakdown. One advantage of world-wide subscibers; some-one else can answer while I sleep ....
 
2 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
`ls -ltra | cut -f3` How would you cut? frenchn00b Linux - Newbie 11 06-13-2011 02:54 AM
help with cut command using find. Cut last 8 characters leaving the rest ncsuapex Programming 4 09-16-2009 08:55 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration