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

can't pull lines from a file where NOT = to a searchstring

I want to search a file for a string, then ignore it and pull out entries that don't contain the string. I've tried lots of things but can't seem to make it work.
file is format is


xxxxxxxxxxx||xxx||xxxxxxx||xxxxx||searchstring||xx xxxx|\xxxxxx <-- dump these
xxxxxxxxxxx||xxx||xxxxxxx||xxxxx||xxxxxxxxxx||xxxx xx|\xxxxxx <--need these lines

using this code works to find the string using =~ but using !~ doesn't seem to do anything

thanks


Expand|Select|Wrap|Line Numbers
  1. ##############################################################
  2. #Get rid of GP entries
  3. ##############################################################
  4.  
  5. open (FHNDL, ">$errorfile") || die "ERROR Could not open file!\n";
  6. open(SEARCHFILE, "<$tempfile1") || die "Could not open file!\n";
  7. while (<SEARCHFILE>)
  8.      {
  9.            if ($_ !~/$searchstring3/)
  10.               {
  11.                print FHNDL $_;
  12.               }
  13.        }
  14.  
  15. close (FHNDL);
  16. ###############################################################
  17.  
Sep 7 '07 #1
5 1360
numberwhun
3,509 Expert Mod 2GB
Just a quick look at your code does not tell me what $searchstring3 is getting set to. I wonder if you have to escape the $ because in regular expressions, the $ is a special character meaning match the end of line. You may have to put a \ before it to escape it.

I just tried the following code against your sample dump and it finds only the line without the search string.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. open(FILE, "<./dump.txt");
  5.  
  6. while(<FILE>)
  7. {
  8.     if($_ !~ m/\|\|searchstring\|\|/ig)
  9.     {
  10.         print("$_");
  11.     }
  12. }
  13.  
Regards,

Jeff
Sep 7 '07 #2
KevinADC
4,059 Expert 2GB
it looks like it should work. Post some sample data and sample search string.

Jeff:

perl interpolates scalars and arrays in matching regular expressions, so you would not want to escape the $ symbol if that is a scalar being used for the search pattern. When a bare '$' is at the end of the pattern that indicates end of string anchor:

Expand|Select|Wrap|Line Numbers
  1. if (/$foo$/) {
  2.    print
  3. }
anyplace else and it is interpreted as a scalar.
Sep 7 '07 #3
numberwhun
3,509 Expert Mod 2GB
I knew that it did, but couldn't remember if you had to escape the $. I know at the end of the regex what it means, but didn't know if it should be escaped otherwise.

Well, the code I posted does work, so I would definitely do a check to ensure that the variable is being set to something that exactly matches what is in the unwanted lines by using a print statement then.

Regards,

Jeff
Sep 7 '07 #4
KevinADC
4,059 Expert 2GB
OK. Don't escape it unless looking for a literal $ sign in the pattern. :)
Sep 7 '07 #5
Hi, the function does work
I think the problem was a line feed next to the searchstring. I've modified it search a little bit further along thanks for your help
Sep 10 '07 #6

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

Similar topics

3
by: nadia | last post by:
Is it possible to do the following in php: I want to have a main form open. In the form I want a button that will open a popup window so the user can search for something. The user can then select...
1
by: lily82 | last post by:
can sm 1 help me transform this code to C# code?? tq so much :wink: Goto : <% Dim counter Dim page Dim pages counter= 10 pages = 20 page = 1
5
by: Chris R. | last post by:
I'm trying to do something relatively simple - find the offset of the first - or next - occurance of a string in a file, ideally in a case- insensitive way. I've seen a few solutions that seemed...
10
by: mjl1976 | last post by:
I have textBox1 which is the string i want to search in file.txt Button1 I want textBox2 to show the line of text i am searching when i click button1
1
by: Jim Heavey | last post by:
Hello, I am running into something strange. I am getting the following error when the program attempts to execute a particular procedure: An unhandled exception of type...
1
by: midnightblue | last post by:
Please could someone help me. I am a relative newbie, and have the following function on my php search page on my Joomla! site. <?php } } function...
3
by: brigitte | last post by:
The original problem: I need a procedure to import a csv file created by a third party application into an Access database. This file contains fields which may include commas, and when they do,...
0
by: ASPnewb1 | last post by:
Hello, I'm trying to execute a find command on my Excel obj to find if a value exists in only one column, not the whole worksheet, what I had before that works (if the whole worksheet is...
9
by: drhowarddrfine | last post by:
I don't want to use a db manager, like mysql, for such a small database but I'm finding this trickier than I thought and hope someone can provide some guidance. I have a restaurant menu with...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.