473,806 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

12 New Member
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||xx x||xxxxxxx||xxx xx||searchstrin g||xxxxxx|\xxxx xx <-- dump these
xxxxxxxxxxx||xx x||xxxxxxx||xxx xx||xxxxxxxxxx| |xxxxxx|\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 1372
numberwhun
3,509 Recognized Expert Moderator Specialist
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 Recognized Expert Specialist
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 Recognized Expert Moderator Specialist
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 Recognized Expert Specialist
OK. Don't escape it unless looking for a literal $ sign in the pattern. :)
Sep 7 '07 #5
dexter48
12 New Member
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
17068
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 what they want, and the popup window closes and the main form (which is still open) shows the search results. Can anyone suggest a best course of action to acheive this? Thanks
1
1361
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
2185
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 to involve loading the whole file as a string, but that seems to be limiting the size of the file stream that could be handled. Is there a way to do searching with just the stream? Or do i need to start loading the file into string buffers of...
10
25602
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
2835
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 'System.MissingMemberException' occurred in microsoft.visualbasic.dll Additional information: Public member 'FindSearchValueRecord' on type 'ProfileRecordProcessing' not found. This seems to suggest that the function "FindSearchValueRecord" does not
1
1964
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 showSearchForm($dropListsArray,$config,$items,$pageNav,$fieldData,$searchString,$catid,$total,$phrase,$res) { global $mosConfig_live_site,$mosConfig_absolute_path,$my; if(!$searchString) $String = 'Search...'; else
3
5913
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, Access confuses the commas for field separators, so that a block of text which should all be in one field ends up in two fields. I've seen by reading through this group that this is a pretty common problem with csv files. The solution I came up...
0
2467
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 searched): ************************************************************************************* Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application(); Workbook theWorkbook =...
9
1918
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 prices and other info in a small file. It's set up in a YAML-ish style, if you're familiar with that format. I'm just looking for some ideas on the best way to retrieve data based on a "keyword". What complicates things for me is that some of these...
0
9599
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.