LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-18-2012, 06:22 PM   #1
knoweledgeseeker
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Rep: Reputation: Disabled
Unhappy Match a pattern in linux


hello,

I want to look for the below pattern in my file.
x can be any number (one or more digits). Everything else in the paragraph i want it be exactly the same.

I ve been trying to do that for more than 6 hours now :s I i googled this problem and found that there is a grep command line i tried to understand how it works and use it but with no vain. then i tried studying the "regular expressions" but that will take me forever before i know how to do that :s
Can anybody please help me with this ! i am stuck . I need this command bcz it will help me automate the process of computing the the result of my experiments which i need very soon
Code:
Node: x Time: xs Ipv4ListRouting table
  Priority: 5 Protocol: ns3::olsr::RoutingProtocol
Destination	NextHop		Interface	Distance
10.1.1.1           x.x.x.x		x		x	
10.1.1.2	        x.x.x.x		x		x	
10.1.1.3		x.x.x.x		x		x	
10.1.1.4		x.x.x.x		x		x
10.1.1.5		x.x.x.x		x		x
10.1.1.6		x.x.x.x		x		x
10.1.1.7		x.x.x.x		x		x
10.1.1.8		x.x.x.x		x		x
10.1.1.9		x.x.x.x		x		x
10.1.1.10		x.x.x.x		x		x
your help is highly appreciated
 
Old 08-18-2012, 06:57 PM   #2
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
please, show the output you are expecting to get

Code:
$ echo 'Node: 0123 Time: 1s Ipv4ListRouting table' | sed -r 's@ [0-9]+@ _M_A_T_C_H_E_D_@g'
Node: _M_A_T_C_H_E_D_ Time: _M_A_T_C_H_E_D_s Ipv4ListRouting table
 
Old 08-18-2012, 07:04 PM   #3
knoweledgeseeker
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you for your reply .

here is a snippet of the file that I want to look into

Code:
  HNA Routing Table:
  Priority: 0 Protocol: ns3::Ipv4StaticRouting
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
127.0.0.0       0.0.0.0         255.0.0.0       U     -      -      -   0
10.1.1.0        0.0.0.0         255.255.255.0   U     -      -      -   1

Node: 2 Time: 3s Ipv4ListRouting table
  Priority: 5 Protocol: ns3::olsr::RoutingProtocol
Destination		NextHop		Interface	Distance
10.1.1.1		10.1.1.1		1		1	
10.1.1.2		10.1.1.2		1		1	
10.1.1.4		10.1.1.4		1		1	
10.1.1.5		10.1.1.5		1		1	
10.1.1.7		10.1.1.7		1		1	
10.1.1.8		10.1.1.8		1		1	
10.1.1.9		10.1.1.9		1		1	
 HNA Routing Table:
  Priority: 0 Protocol: ns3::Ipv4StaticRouting
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
127.0.0.0       0.0.0.0         255.0.0.0       U     -      -      -   0
10.1.1.0        0.0.0.0         255.255.255.0   U     -      -      -   1

Node: 3 Time: 3s Ipv4ListRouting table
  Priority: 5 Protocol: ns3::olsr::RoutingProtocol
Destination		NextHop		Interface	Distance
10.1.1.2		10.1.1.2		1		1	
10.1.1.3		10.1.1.3		1		1	
10.1.1.5		10.1.1.5		1		1	
10.1.1.8		10.1.1.8		1		1	
10.1.1.9		10.1.1.9		1		1	
10.1.1.10		10.1.1.10		1		1

I want to retrieve all tables in which the destination column have all the ips from 10.1.1.1 to 10.1.1.10
so that is the output i am expecting :
Code:
Node: x Time: xs Ipv4ListRouting table
  Priority: 5 Protocol: ns3::olsr::RoutingProtocol
Destination	NextHop		Interface	Distance
10.1.1.1           x.x.x.x		x		x	
10.1.1.2	        x.x.x.x		x		x	
10.1.1.3		x.x.x.x		x		x	
10.1.1.4		x.x.x.x		x		x
10.1.1.5		x.x.x.x		x		x
10.1.1.6		x.x.x.x		x		x
10.1.1.7		x.x.x.x		x		x
10.1.1.8		x.x.x.x		x		x
10.1.1.9		x.x.x.x		x		x
10.1.1.10		x.x.x.x		x		x
any idea how to get all paragraphs that look like the above expected output ?
 
Old 08-19-2012, 01:53 AM   #4
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
ok, if you want to do it with scripting, then awk tool is what you need, or you can do that with pure bash script reading input line-by-line; though it seems to me that some language like lua/perl/python would fit your needs better
 
Old 08-19-2012, 05:42 AM   #5
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 37
Using some unrefined awk, this might do it...
Code:
awk '/^10\.1\.1\.([0-9]|10)\t/ { x[n++]=$0 }
     /^Node: [0-9]+ Time: [0-9]+s/ {
          if (n>0) { print "\n",z1,"\n",z2,"\n",z3
                     for (j=0; j<n; j++) print x[j] }
          z1=$0; getline; z2=$0; getline; z3=$0; n=0 }
     END{ if (n>0) { print "\n",z1,"\n",z2,"\n",z3;
                     for (j=0; j<n; j++) print x[j] } }' data.dat
Happy with ur solution... then tick "yes" and mark as Solved!

Last edited by dru8274; 08-19-2012 at 06:38 AM.
 
Old 08-19-2012, 07:10 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
then i tried studying the "regular expressions" but that will take me forever before i know how to do that
Well, you are not going to do it without using regular expressions.....I'd recommend taking the time to learn the basics.

When You are trying to match patterns, think carefully about the criteria. For example, if all the IPs start with "10.1.1", then matching them is trivial. (The regular expression required to match any legal IP is a bit messy)
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pattern match number tinkulim Programming 2 06-14-2012 02:51 AM
[SOLVED] Adding (not replacing) a pattern match with a similar pattern? b-bri Linux - Newbie 2 08-31-2009 12:36 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:08 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