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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with white spaces 3

Status
Not open for further replies.

LotoL

Programmer
Mar 17, 2004
20
CA
Hello, I need help with white spaces. Im try to search for the input listed below called Database name. Im very new to all this. Also any clues on how to search for the first line then get the Node name called QIBS34.


code -

if (/\bDatabase name\s=\s$DB_Name/)


input -
Database name = IBSQ35
Node name = QIBS34
 
Code:
\s+

in terms of looking for the second line, can you give us some detail on what you've tried so far

--Paul
 
Wouldn't \s+ presume the existence of at least one whitespace character?
 
Isn't that safe considering we're looking for whitespace?

--Paul
 
PaulTEG, this what i have so far. I now that Im not going the right way.. Im to new at this. But here is what I have..

input file -
Database name = IBSQ35
Node name = QIBS34


while (<DB_list>)
{
if (/\bDatabase name\s+=\s$pattern\b/)
{
print ("found it - $pattern \n");
if (/\bNode name =\b/)
{
print ($1);
print ("go it, but did not print variable");
}
}
}
 
What if the input were:
Database name= IBSQ35

I don't knoe that this will happen but I don't know that it won't either.
 
Assuming this is a log file, and spaces are fixed, if you're new to it, why go so complex at first byte...this should work

Code:
open DB_list, "<databases.txt";
while (<DB_list>) {
  if (substr($_, "Database name") != -1) { #Database name exists in string
    $nextline=<DB_list>; #read next line from handle
      $nodename=trim(substr($nextline,34, (length($nextline)-34); 
      print "$nodename \n";
    }
  }
}
close DB_list;
sub trim {
  my @out= @_;
  for (@out) {
    s/^\s+1//;
    s/^\s+$//;
  }
  return wantarray ? @out : $out[0];
}

HTH
--Paul
 
Just copped a typo on $nodename=trim, close out the correct number of parentheses

--Paul
 
Ok. I tried it.. but got an error see below.

here is my full code
print ("Enter the search pattern:\n");
$pattern = <STDIN>;
chop ($pattern);
open(DB_list, "/home/GetNode/listdb.txt");

print ("Matches found:\n");

while (<DB_list>)
{
if (substr($_, "Database name") != -1)
{ #Database name exists in string
$nextline=<DB_list>; #read next line from handle
$nodename=trim(substr($nextline,34,(length($nextline)-34)));
print "$nodename \n";
}
}
close DB_list;
sub trim {
my @out= @_;
for (@out) {
s/^\s+1//;
s/^\s+$//;
}
return wantarray ? @out : $out[0];
}


output :-(
Enter the search pattern:
IBSQ35
substr outside of string at ./port1 line 16, <DB_list> line 2.
 
3:30 am GMT
Racing festival-->bad idea
2 morow, sort it out
--Paul
 
Give this a shot:

Code:
print ("Enter the search pattern:\n");
my $pattern = <STDIN>;
chomp ($pattern);
open(DB_list, "< ./listdb.txt");

print ("Matches found:\n");

while (<DB_list>) {
	if (m/$pattern/i) {
		print "DB: $pattern - ";
		my $nextline=<DB_list>;
		$nextline =~ m/.+=\s*(\w+)\b/i;
		print "Node: $1\n";
    }
}
close DB_list;
 
thanks rharsh,

it still does not read the second line with the node name. the output is DB: IBSQ35 - Node: IBSQ35. it should be
DB: IBSQ35 - Node: QIBS34.


I tried changing the print "Node: $1\n"; to print "Node: $4\n"; and it still gives me the pattern "IBSQ35"

btw, what is the debug syntax to see how the script works to show me line my line.


input -
Database name = IBSQ35
Node name = QIBS34
 
rharsh's code works for me

--Paul

listdb.txt--
Database name = IBSQ35
Node name = QIBS34

Database name = IBSQ3500
Node name = QIBS33

Database name = IBSQ3505
Node name = QIBS32

Database name = IBSQ35067
Node name = QIBS37
--
Code:
my $pattern = "IBSQ3505";

open(DB_list, "<listdb.txt") or die "$!\n";

print "Matches found:\n";

while (<DB_list>) {
    if (m/$pattern/i) {
        print "DB: $pattern - ";
        my $nextline=<DB_list>;
        $nextline =~ m/.+=\s*(\w+)\b/i;
        print "Node: $1\n";
    }
}
close DB_list;
 
I tested the code and it works fine for me. $1 is the right variable, but it appears that the second regex must not be matching anything. To figure out what's going on, you'll probably need to post more info from your input file. Assuming it looks similar to what Paul used, the code works fine.
 
Code:
$pattern = "IBSQ35";

print "pattern to search for : ";
chomp ($pattern = <>);

undef $/;

$data = <DATA>;

$data =~ m|^Database name[^=]+= $pattern[^=]+= ([A-Z0-9]+)|sm;

print "Node name is : $1\n";

__DATA__
Database name                        =    IBSQ3500
Node name              =      QIBS33 

Database name              =   IBSQ35
Node name                                  = QIBS34
 
Database name                   = IBSQ3505
Node name     = QIBS32 

Database name     = IBSQ355678067
Node name         = QIBS377777

Database name     = IBSQQQQQ35067
Node name         = QIBS378907

Database name                   = IBSQ35067
Node name         = QIBS37


Kind Regards
Duncan
 
Thanks for all the feed back guys...

I'll try it some time next week...


 
Good job Guys but when I use the input file below it does not work.. I only give me "DB: I34Q23SB - Node: I34Q23SB"



Database 1 entry:

Database alias = I34Q23SB
Database name = I34Q23SB
Node name = QIST23SB
Database release level = 9.00
Comment =
Directory entry type = Remote
Catalog node number = -1

Database 2 entry:

Database alias = BDBC9
Database name = BDBC9
Node name = STPRE
Database release level = 9.00
Comment =
Directory entry type = Remote
Catalog node number = -1

Database 3 entry:

Database alias = ITRIN
Database name = ITRAN
Node name = ISSTS
Database release level = 9.00
Comment =
Directory entry type = Remote
Catalog node number = -1
 
The regex in Duncan's code should work great. All that needs to be done to fix my code is change:

Code:
if (m/[red]$pattern[/red]/i) {

to:

Code:
if (m/[blue]Database name.+$pattern[/blue]/i) {

and it should work just fine.
 
it was a bit difficult to guess how your data was structured...

this works with the real-life data you posted:-

Code:
$pattern = "IBSQ35";
print "pattern to search for : ";
chomp ($pattern = <>);
undef $/;
$data = <DATA>;
$data =~ m|Database name[^=]+= $pattern[^=]+= ([A-Z0-9]+)|sm;
print "Node name is : $1\n";

[blue]__DATA__
Database 1 entry:
 Database alias                  = I34Q23SB
 Database name                   = I34Q23SB
 Node name                       = QIST23SB
 Database release level          = 9.00
 Comment                         =
 Directory entry type            = Remote
 Catalog node number             = -1
Database 2 entry:
 Database alias                  = BDBC9
 Database name                   = BDBC9
 Node name                       = STPRE
 Database release level          = 9.00
 Comment                         =
 Directory entry type            = Remote
 Catalog node number             = -1
Database 3 entry:
 Database alias                  = ITRIN
 Database name                   = ITRAN
 Node name                       = ISSTS
 Database release level          = 9.00
 Comment                         =
 Directory entry type            = Remote
 Catalog node number             = -1[/blue]

example of script working:-

Code:
[red]pattern to search for : BDBC9
Node name is : STPRE[/red]


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top