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

Searching string in a file

Status
Not open for further replies.

NesPes

Programmer
Dec 15, 2007
17
US
i have a very basic question. Since i am new to Perl, i am unable to resolve this myself. Any help will be greatly appreciated.

I am browsing through a file and searching for a string. If the string matches, i want to pick the next line. Somehow i am not able to move my file handler to the next line after pattern matches.

Input Ex:
sis_admin_role("Enter Name",
`Enter Current role,
`Enter Age);

Expected output: Enter Current role.

My code:

while(<f0>) {
chomp;
if($_ =~ /Name/) {
$next_line = <f0>;
print "$next_line[0]\n";
}
}

 
$next_line is a scalar variable. What you're printing is $next_line[0], which is the first element in the array @next_line, which is very different to $next_line.
 
Thanks for the reply. I got it.
I have one more doubt. i am trying to search for a string in a file which is defined as

`define ABCD `AB.BC.DA

I want to grep for the string ABCD in the file and if this is found, then the script should print AB+BC+DA

Can you please help me on this?
 
Code:
open FH, "<filename.ext"
while (<FH>) {
  if (/ABCD/) {
    print "AB+CD+DA"; # no but seriously, here's where you parse the line to determine what you actually need
  }
}
close FH;

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Is ths school/class work? This same question is on another forum, and the code you posted appears to be taken from that thread:

Code:
while(<f1>) { 
   if(/dogy/) { 
      $next_line = <f1>; 
      #do something with $next_line; 
   } 
}

and the sample data is the same.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I would like to print scc.ecm.chip if the string ABCD is found in the file. Your inputs.

`define ABCD `scc.ecm.chip

Thanks in advance.
 
please answer my question concerning school/class work.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I have not posted my query on any other forum. I do not know if some-one else has a similar question.
As for me, i want to learn Perl and use it in my day-to-day work. It is not part of any school/class work.

Thanks.
 
the other thread (posted yesterday) is titled:

Issue with search and capture string from a file

very similar to yours.

Searching string in a file

The other thread starts with:

This is the contents of my input file. I want to browse through this file and look for "im_im_rdy_samp". If this keyword is found, i want to then pick the string on the next line (in this case FRST_IM_FILE_IO_AMP) and store it in a variable.

dogy #1050 fi_dy_watch ("im_im_rdy_samp",
`FRST_IM_FILE_IO_AMP.Q,
`FRST_IM_FILE_IO_AMP.D,
`FRST_IM_FILE_IO_AMP.`CP,
`FRST_IM_FILE_IO_AMP.`flag);

//---------------Code

while(<f1>) {
chomp;
if($_ =~ /dogy/)
{
@temp = split(/\(\"/, $_);
@temp = split(/\"/, $_);
chop($temp[1]);
$sync_cap = $temp[1];
print "$sync_cap\n";
}
if($_ =~ $sync_cap)
{
@temp = split(/`/, $_);
chop($temp[0]);
print "$temp[0]\n";
}
}


With the raw code posted above, i am able to search for the string (im_im_rdy_samp) and the code displays the first line (fi_dy_watch).

I am not able to go to the next line and pick the string starting with this "`" character.

I am pretty new to Perl. Can some-body please help me with this?

Thanks,

wraz

my answer a little later in the thread:

Code:
while(<f1>) { 
   if(/dogy/) { 
      $next_line = <f1>; 
      #do something with $next_line; 
   } 
}

which is almost the exact same code you posted above:

Code:
while(<f0>) {
       chomp;
       if($_ =~ /Name/) {
          $next_line = <f0>;
          print "$next_line[0]\n";
         }
       }

I turst you can see my reason for thinking you are the same person posting on both forums and why I believe this is school work.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
The query on the other forum is similar to mine. But it seems to be pure coincidental to me. To answer again, this is not part of any school work, i am a working professional who is bent to learn perl to make life easier. I have nothing more to say. If you beleive me, please help me with my query.
 
show us what you have learned thus far ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
So far I have learnt how to traverse a file and search for given strings within a file.
 
All you need to to expand on PaulTEG's example to include the use of a regex with extracted matches.

Also, be sure to include use strict at the beginning of every script. That would have detected the original bug that ishnid caught for you.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file[/blue] = [red]"[/red][purple]filename.ext[/purple][red]"[/red][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] [red]([/red][black][b]my[/b][/black] [blue]$fh[/blue], [blue]$file[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$file[/blue] :[blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<[blue]$fh[/blue]>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
	[olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]`define ABCD[purple][b]\s[/b][/purple]+`([purple][b]\S[/b][/purple]+)[/purple][red]/[/red][red])[/red] [red]{[/red]
		[black][b]my[/b][/black] [blue]$value[/blue] = [blue]$1[/blue][red];[/red]
		[blue]$value[/blue] =~ [red]s/[/red][purple][purple][b]\.[/b][/purple][/purple][red]/[/red][purple]+[/purple][red]/[/red][red]g[/red][red];[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$value[/blue][red];[/red]
	[red]}[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] [blue]$fh[/blue][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top