473,385 Members | 1,343 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,385 software developers and data experts.

search string in a file and print

Hi,

I have a file which has contents like this :

Indicators :

for issues with indicators contact a@x.com

for access to indicators contact b@x.com

Unix_Versions :

for latest unix versions check out u.versions.com/new.html

for past versions please see u.versions.com/old.html

-----------------

The keywords are indicators & unix versions

if somebody runs a perl script with any of the keywords, they should the lines corresponding to that.

Say they do, test.pl unix_versions, the 2 lines in the above file below the keyword unix_versions should be printed on screen.

---------------



how to do that ?

I think I need to read in the above file into an array.But am not sure how to go about after that.
Dec 9 '08 #1
3 10130
numberwhun
3,509 Expert Mod 2GB
Ok, so what have you tried thus far? Why not post your code here (encosed in code tags) and we will help you from there.

Regards,

Jeff
Dec 9 '08 #2
Note: This could be the code if the content was one line each and the keyword was enclosed(2nd word in ech line) as part of that line.But what I need is if that key word is separate line as mentioned in the original question and there is more than one line below the keyword.

Expand|Select|Wrap|Line Numbers
  1.  
  2. ## assuming below is how file1.txt looks like ## 
  3. ## For Indicators, please visit b.x.com. 
  4. ## For unix_versions, visit bee.versions.com/new.html 
  5. ## For file_access issues contact xxx@y.com. 
  6. ################################################################# 
  7.  
  8. #!/usr/bin/perl 
  9.  
  10. $request =<STDIN>; 
  11. chomp($request); 
  12.  
  13. $file = "/nfs/.../.../.../file1.txt"; 
  14.  
  15. open (FILE1, $file) || die ("unable to open file: $!"); 
  16.  
  17. @file1 = <FILE1>; 
  18.  
  19. foreach $line (@file1) { 
  20. chomp ($line); 
  21. @words = split(/\s+/,$line); 
  22. if ($request eq $words[1]) { print "$line \n" ; } } 
  23.  
  24. close <FILE1>;
  25.  
Dec 9 '08 #3
KevinADC
4,059 Expert 2GB
may as well post my solution here too:

Expand|Select|Wrap|Line Numbers
  1. $request =<STDIN>;   
  2. chomp($request);   
  3.  
  4. $file = "/nfs/.../.../.../file1.txt";   
  5. my @lines = ();  
  6. open (FILE1, $file) || die ("unable to open file: $!");   
  7.  
  8. LOOP: while (<FILE1>) {  
  9.    if (/^$request :/i) {  
  10.       while (<FILE1>) {  
  11.          last LOOP if /^\w+ :/;  
  12.          push @lines,$_;  
  13.       }  
  14.    }  
  15. }  
  16. close <FILE1>;  
  17. print "@lines";
  18.  
Dec 10 '08 #4

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

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
10
by: Bulba! | last post by:
Hello everyone, I'm reading the rows from a CSV file. csv.DictReader puts those rows into dictionaries. The actual files contain old and new translations of software strings. The dictionary...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
1
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
10
by: B. Williams | last post by:
I have an assignment that requires me to write a program that uses a class, a constructor, a switch, and store the records in a text file. The second requirement is to create a function called...
2
by: Ola K | last post by:
Hi guys, I wrote a script that works *almost* perfectly, and this lack of perfection simply puzzles me. I simply cannot point the whys, so any help on it will be appreciated. I paste it all here,...
15
by: djtosh | last post by:
hi there hoping someone can help me out with this. im designing a code that is a basic database holding peoples details and i need to implement a method that searches by txt for a record by their...
1
by: nganglove | last post by:
C++ string search -------------------------------------------------------------------------------- Hello, please can any one help me? I am given an assigment in C++ to read a text file and...
4
by: ravindarjobs | last post by:
hi...... i am using ms access 2003,vb6 i have a form. in that i have 2 buttons 1. start search 2 stop search when i click the "start search" button the fucntion SearchSystem() is called,...
1
by: 848lu | last post by:
hi, im trying to do a search on my Array, where a users searches on a HTML screen and then the PHP searches the array for the data under the roice entred by user.....text file data is under the php...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.