Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

command to find a paraprah in a file in linux

Status
Not open for further replies.

knowledge_Seeker1

Newbie level 6
Joined
Jan 22, 2012
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,382
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 3 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.
Can anybody please help me with this ! i am stuck . I need to do find the result of my experiments very soon :(
HTML:
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
 
Last edited:

You actually want to use regular expressions (regexp), but although they can be used with the 'grep' command,
you actually need something else, maybe 'sed' for multi-line.
Grep is ideal if you want to check a single line .
I'd suggest, are you sure that you need to match on the entire paragraph, or just the first line?
(looks just like a netstat dump).
Anyway, at least you now know to look at sed documentation and regexp. I'd suggest it may take you
a day to get some practice (I'm no expert with sed or regexp).
For example, if you wanted to match just a single line, e.g. the 10.1.1... line that you have, then you'd use something like:
grep -n -i"10.1.1.[0-9]*[ \t]*[0-9]*.[0-9]*.[0-9]*.[0-9]*" *
and that would check most of the first line (you could extend further if you think you really need to match further.
The example above is fairly self-explanatory.
Anything more complex, you really need to study regular expressions (I am not an expert, and I tend to
consult a book when I need to do any more complex regexp). The best reference work is "Mastering Regular Expressions"
by Freidl.
 
Thank you for your reply, Its been a couple of hours that i am trying to know how to do it with sed by i cant seem to get this working :S
unfortunately i cant afford to spend a couple of days on this because i have a deadline to submit the results of my simulation and i really need this command :s
can anybody please help me :(?
 

Ok, this should work for you (I reverted to a language I'm better at than sed/regexp).
This C code does a _partial_ check, which should be sufficient to identify the structure of the paragraph (but not the entire detail).
Maybe that is sufficient.
Code:
#include <stdio.h>


FILE* f;

int
main(void)
{

  char tbuf[1000];
  int i;
  int row=0;
  int rowadd;
  int found;

  f=fopen("testfile.txt", "r");
  if (!f)
  {
  	printf("Error opening file\n");
  	return(1);
  }

  fgets(tbuf, 999, f);
  row++;
  while (!feof(f))
  {
    found=0;
    rowadd=0;
    
    printf(".");
    if (strncmp("Node:", tbuf, 5)==0)
    {
      fgets(tbuf, 999, f);
      rowadd++;
      if (strncmp("  Priority:", tbuf, 11)==0)
      {
        fgets(tbuf, 999, f);
        rowadd++;
        if (strncmp("Destination", tbuf, 11)==0)
        {
          fgets(tbuf, 999, f);
        	rowadd++;
        	if (strncmp("10.1.1.1", tbuf, 8)==0)
        	{
        	  printf("\nMatch found on line %d\n", row);
        	  found=1;
        	}
        }
      }
    }
    else
    {
    	fgets(tbuf, 999, f);
    	row++;
    }
    row=row+rowadd;
    rowadd=0;
  }
  
  fclose(f);
  return(0);
  
}

I ran this code with a test file (needs to be called testfile.txt):
Code:
the quick
brown fox
jumped
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
over the
lazy dog
Node: x Time: xs Ipv4ListRouting table
  Priority: 5 Protocol: ns3::olsr::RoutingProtocol
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

This was the output:
Code:
filecheck.exe
....
Match found on line 4
..........
Match found on line 17
........

You could play around with the source code to make it a better file inspection if this one is inaccurate.
I've attached a windows executable (worked for me on Win7) and the test file that I used.

View attachment filecheck.zip
View attachment testfile.txt
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top