473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Perl Script to Test for Condition

Hi,

I have a file that contains the following...

hostMode=Standard
displayName=CL1-A-0
nickname=1A-G00
hostMode=Windows Extension
displayName=CL1-A-2
nickname=SERVER123_B20_S6_0

I have a perl script (Below) that reads the above file, and if it finds the instance of "hostMode=Standard" it prints it out, (this is just to prove my sytax works).
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. open (FILE,"mp.txt") || die print "Can't Open File\n";
  3. while (<FILE>) {
  4.     $_=~ s/^\s+//;
  5.     if (/hostMode=Standard/) {print "$_";}
  6.                }
  7. close (FILE);
  8.  
What I'd like to do is... if "hostMode=Standard" is found, I'd like to jump 2 lines down in the file and test if the first character of "nickname=" is a string. If it is I'll print it out, if it's not I'll just loop again for the next occurance of "hostMode=Standard" and do the same. Any ideas?
Sep 1 '08 #1
2 1926
Ganon11
3,652 Expert 2GB
If you do a regular expression over multiple lines and match for the "hostMode=Standard" string, a line containing whatever, and "nickname=", then capture whatever remains on that line, you can compare away.
Sep 1 '08 #2
nithinpes
410 Expert 256MB
To do a regular expression over multiple lines, you need to undefine $/ first to read entire file at once and use /g option in pattern match to do the match repeatedly.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. open (FILE,"mp.txt") || die print "Can't Open File\n";
  3. $/="";  # undefine input record separator
  4. while (<FILE>) {
  5. $_=~ s/^\s+//;
  6. while (/hostMode=Standard\n.+\n(nickname=(.).*\n)/g) {
  7. $f=$1;$ch=$2;
  8. print "$f" if($ch =~/[A-Z]/i);} #print if 1st character after 'nickname=' is alphabet
  9.  }
  10. close (FILE);
  11.  
Sep 2 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Marc H. | last post by:
Hello, I recently converted one of my perl scripts to python. What the script does is simply search a lot of big mail files (~40MB) to retrieve specific emails. I simply converted the script...
3
by: David F. Skoll | last post by:
Hi, I'm tearing my hair out on this one. I'm trying to embed a Perl interpreter into a C program. I need to be able to create and destroy the interpreter periodically, but will never actually...
2
by: David McBride | last post by:
I'm getting the following message when I run my Perl script that uses the XML::Writer module: "Processing instruction target begins with 'xml' at .../makeconf/LW/LWTest.pm line 75" The pi()...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
2
by: manicmax | last post by:
Hi Frds, I am new to perl and cgi. I am posting data and i want to use that data in my test.pl through cgi.I have 2 questions. First one is...
9
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
22
by: owlice | last post by:
Greetings! I thought I'd add a little something to a web site, a "tip of the week," and wanted it automated so that if I get hit by a truck (or, more likely, am forgetful), the tip is updated...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.