Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grep Command Help

Status
Not open for further replies.

TamedTech

IS-IT--Management
May 3, 2005
998
GB
Hello Guys,

I'm looking for a little assistance with a grep script for grabbing information from a command output and then comparing it to a text file.

Basicly i have an output from a command i've run that looks similar to the one below, and i'm looking to use grep and cut to remove all instances of the sections highlighted in red, then compare it to the list from a text file.

I then want to select the first instance that does NOT appear in the text file and then use that as a variable.

THE OUTPUT FROM MY COMMAND
Code:
Searching for opush on [COLOR=red]00:09:2D:31:69:4C[/color] ...
Service Name: OBEX Object Push
Service RecHandle: 0x10008
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 2
  "OBEX" (0x0008)
Language Base Attr List:
  code_ISO639: 0x656e
  encHoding:    0x6a
  base_offset: 0x100
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0100

Searching for opush on [COLOR=red]00:0E:6D:04:6D:06[/color] ...
Service Name: OBEX Object Push
Service RecHandle: 0x10004
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 9
  "OBEX" (0x0008)
Language Base Attr List:
  code_ISO639: 0x656e
  encoding:    0x6a
  base_offset: 0x100
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0100

MY TEXT FILE
Code:
00:0E:6D:04:6D:06
00:0E:6D:04:6D:07
00:0E:6D:04:6D:08
00:0E:6D:04:6D:09

So for instants with the example above, i would like the code to return 00:09:2D:31:69:4C as it is not present in the text file.

I know its a little complicated and i've probably not explained myself particularly well, but i'm new to Linux so you'll have to excuse my niavity.

Please let me know if there is any extra information i can pass your way to help with this.

I've tried using GREP as i know it and can only get it to return me the first instance of the search criteria when i really need a list of all instances within my command output.

Thanks for any help you can give me.

Rob
 
Thanks thedaver,

I conquored that about the same time as you were posting.

The next bit is the tricky one, now i need to take the list of content output by the GREP and compare it against my .txt file and find one of the list that is NOT in the text file and set that as a variable.

Ta,

Rob
 
Hello again guys,

I've now sorted my 'Compare' facility using a COM statement to compare the two files, works a charm.

I need a little assistance though, as i need to re-asses that GREP statement at the begining, and i'm now looking at using egrep to grab several searches.

I wanted to take the following output:

Code:
Searching for opush on [COLOR=red]00:09:2D:31:69:4C[/color] ...
Service Name: OBEX Object Push
Service RecHandle: 0x10008
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: [COLOR=red]2[/color]
  "OBEX" (0x0008)
Language Base Attr List:
  code_ISO639: 0x656e
  encHoding:    0x6a
  base_offset: 0x100
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0100

Searching for opush on [COLOR=red]00:0E:6D:04:6D:06[/color] ...
Service Name: OBEX Object Push
Service RecHandle: 0x10004
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: [COLOR=red]9[/color]
  "OBEX" (0x0008)
Language Base Attr List:
  code_ISO639: 0x656e
  encoding:    0x6a
  base_offset: 0x100
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0100

Then take the sections highlighted in red and display them as follows.

Code:
00:09:2D:31:69:4C-2
00:0E:6D:04:6D:06-9

I've been using "egrep 'Searching|Channel'" to try and get it working but having real issues getting my Cut commands to trim the content and arrange it as i would like.

Any more help you can offer would be really appreciated.

Thanks again,

Rob
 
I realise you asked for a grep solution, but this is getting to the point where stringing together a bunch of assorted utilities won't cut it any more.
Code:
#!/usr/bin/perl
use strict;
use warnings;

my %exclude;
my %opush;
my $id;

while (<DATA>) {
    chomp;
    $exclude{$_}++;
}
open(OPUSH, "./opush.txt") or die "$!";

while (<OPUSH>) {
    chomp;
    $id = $1 if /Searching for opush on ([0-9A-F:]+)/;
    $opush{$id} = $1 if (/Channel: (\d+)/ and !(exists $exclude{$id}));
}

foreach (sort keys %opush) {
    print "$_-$opush{$_}\n";
}

__DATA__
00:0E:6D:04:6D:06
00:0E:6D:04:6D:07
00:0E:6D:04:6D:08
00:0E:6D:04:6D:09
The above script reads the exclusion list (inline data in this example, but you can always use a file) and then reads through the OPUSH data storing the ids and the channel numbers that aren't in the exclusion list. Then it prints them out.

If you have the command and its parameters, we can make it even easier by having perl invoke the command directly, bypassing the file altogether.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hello Steve,

Thanks for the help, I dont mind not using the grep statement if it gets the job done.

I've been messing around all morning with the 'tr' command after my 'grep' to try and strip out everything i dont need, but its taking a little bit of work.

As for my command it is as follows.

Code:
sdptool search opush

That commans halus back the output you've seen above which i want to strip my details from.

So if we can have that happen, and then display the data as i want it using your methos then that would be excelent.

I used to know a little perl when working as a web developer but its a little lost on me now.

I look forward to hearing a little more on your solution.

Thanks,

Rob
 
Code:
#!/usr/bin/perl
use strict;
use warnings;

my %exclude;
my %opush;
my $id;

open(XCLUDE, "./opush-excludes.txt") or die "$!";

while (<XCLUDE>) {
    chomp;
    $exclude{$_}++;
}

my @cmdoutput = qx{sdptool search opush};

foreach (@cmdoutput) {
    $id = $1 if /Searching for opush on ([0-9A-F:]+)/;
    $opush{$id} = $1 if (/Channel:\s+(\d+)/ and !(exists $exclude{$id}));
}

foreach (sort keys %opush) {
    print "$_-$opush{$_}\n";
}
ought to do it. You will have to create the exclusion file, even if it's empty. It's untested as I don't have sdptool.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks for this steve,

I'm having problems running this script as i inherited the box and it doesnt appear to have perl in the bin directory anywhere.

Is there anyway we can achieve this same effect using standard linux commands? so i can just run it as if i were in a standard command prompt.

Like i say i've been dabbling with the GREP commands all morning but i'm at the completely limitations of my Linux knowledge now and its giving me brain ache.

Like I say i've got a working solution for getting a list of those address's and comparing them against another .txt file and presenting me with the remainders with no trouble at all.

Any chance you can help on a non-perl solution to the problem? it would just make life alot easier for me at this point in the game.

Thanks again steve.

Rob
 
OK, so i've just leared somthing new.

The type of script files i've been generating are known as Shell Scripts!?!? i dont know, i've been using #!/bin/sh at the top of them if that helps any one.

I've done some more hunting and found the 'Sed' command, which looks to be very powerfull but the documentation is not very thorough, and as a newbie seems over complicated.

Thanks,

Rob
 
A linux box without perl? That's like a pub with no beer! You don't say what distro, but if it's Redhat or Fedora you could try rpm -qa | grep -i perl, or even just type perl -V or which perl to see if it's installed.

I'm sure you can do what you want with regular unix utils - awk is probably a good place to start as it's more 'program-like' than the others, but it's real old-school unix in terms of the learning curve. Try to get perl installed.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hi Steve,

Thanks for your help but think i've finally managed to solve my issue.

I dont know whats going on with Perl, and i dont know what distri version i'm running, it doesnt matter though as i've got my script working nicly.

Code:
sdptool search opush | egrep 'Searching|Channel' | sed -e '$!N;s/\n//' -e 's/Searching for opush on //' -e 's/ ...    Channel: /-/'

Is the finall code i managed to get working.

I have simply then been running it in a shell script along with a comm statement to compare it to my exclusion list.

If anyone can think of a way to optemise the code then please let me know as its a little slow.

Thanks,

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top