How to delete multiple lines in a file using perl
I have a file looks like the following:
digraph topology { "192.168.3.254" -> "10.1.1.11"[label="1.000", style=solid]; "192.168.3.254" -> "10.1.1.12"[label="1.000", style=solid]; "192.168.3.254" -> "10.1.1.10"[label="1.000", style=solid]; "192.168.3.254" -> "10.1.1.9"[label="1.000", style=solid]; (skip some lines...) "10.1.1.9" -> "10.1.1.10"[label="1.000"]; "10.1.1.9" -> "10.1.1.11"[label="1.024"]; "10.1.1.9" -> "10.1.1.12"[label="1.076"]; "10.1.1.9" -> "192.168.3.254"[label="1.000"]; "10.1.1.10" -> "10.1.1.9"[label="1.000"]; "10.1.1.10" -> "10.1.1.11"[label="1.020"]; "10.1.1.10" -> "10.1.1.12"[label="1.067"]; "10.1.1.10" -> "192.168.3.254"[label="1.000"]; "10.1.1.11" -> "10.1.1.9"[label="1.028"]; "10.1.1.11" -> "10.1.1.10"[label="1.028"]; "10.1.1.11" -> "10.1.1.12"[label="1.053"]; "10.1.1.11" -> "192.168.3.254"[label="1.000"]; "10.1.1.12" -> "10.1.1.9"[label="1.099"]; "10.1.1.12" -> "10.1.1.10"[label="1.085"]; "10.1.1.12" -> "10.1.1.11"[label="1.057"]; "10.1.1.12" -> "192.168.3.254"[label="1.000"]; "192.168.3.254" -> "10.1.1.9"[label="1.000"]; "192.168.3.254" -> "10.1.1.10"[label="1.000"]; "192.168.3.254" -> "10.1.1.11"[label="1.000"]; "192.168.3.254" -> "10.1.1.12"[label="1.000"]; "192.168.3.254" -> "192.168.3.0/24"[label="HNA"]; "192.168.3.0/24"[shape=diamond]; } I need to search some particular lines and delete them. For example, I need to delete following lines: "10.1.1.9" -> "10.1.1.11"[label="1.024"]; "10.1.1.11" -> "10.1.1.9"[label="1.028"]; "10.1.1.12" -> "10.1.1.11"[label="1.057"]; "10.1.1.11" -> "10.1.1.12"[label="1.053"]; "192.168.3.254" -> "192.168.3.0/24"[label="HNA"]; "192.168.3.0/24"[shape=diamond]; Order of these lines are random... So I cannot delete line #19, for example... And you can see that top four lines I want to delete are pairs. So there might be some clever way to detect the lines, if a line has both "1.9" and "1.11", then delete the line... I am new to perl language. The following is the code I have now... I think I just need to write some code inside the while loop checking if I want to delete the line $dotline before I write to a NEW file. Code:
#!/usr/bin/perl -w |
I'm not sure I understood your criteria fully ... ?!
Getting rid of (not printing) lines that have both 1.9 and 1.11 in them: Code:
printf NEW "$dotline" if($dotline !~ /\.1\.9/ && $dotline !~ /\.1\.11/); Cheers, Tink |
So not in your example of things to be removed, but using the logic you have explained and Tink's example, you would be removing:
Quote:
|
Thank you for reply, Tinkster.
But that deletes all lines with ".1.9" OR ".1.11" grail It did not remove the very first entry in my example. It removed any lines with ".1.9" OR ".1.11" Code:
#!/usr/bin/perl -w |
My bad - my predicate logic went bad once again; replace the && w/ ||.
Cheers, Tink |
Yes, it works with "||"
Is there a way to make code simpler or cleaner? Now, I have a code looks like following: Code:
#!/usr/bin/perl -w |
Code:
while( $dotline = <DOT> ) { Should do the same job ... But: beware - your simplification (omitting the leading \.1) may remove more than you expected inadvertently. Cheers, Tink |
I know I am a relative noob here, but did I miss a split or something?
I am struggling to follow why we are testing $dotline twice? Code:
while( $dotline = <DOT> ) { |
I'm guessing you don't use a lot of Perl? (Assuming I've got your qn correct)
The Code:
while() {} |
Hi Chris
No I was more asking why the others seem to be testing $dotline twice, ie looking at posts #6 and #7? But thanks, I did know that :) cheers grail |
Quote:
Here is my updated code which does exactly what I want it to do. I colored the codes so that it is easier to see. Number of lines is not too much. But I want to know if I could use other variables and while loops to reduce number of lines... I know that when I have a working code, then I am not supposed to edit it until it does not work. But there is possibility that these lines could get much larger. Code:
while( $dotline = <DOT> ) { |
Well I am not 100% sure I am on the right path, but how about:
Code:
while( $dotline = <DOT> ) { |
Quote:
|
@grail ;)
Well I'm a bit confused because the OP said Quote:
Maybe I'm misunderstanding the post#1, but it seems to me he's got a (possibly) large file of recs and wants to remove a smaller subset, contained in another file. Assuming (as per example) that the recs in both files are exact matches (for those in both files), I'd create hash using the smaller set as hash keys, then read through the large file once and for each (large file) rec, check if it's in the hash of recs to be deleted. If so, get next (large file) rec, else output (large file) rec to new file. This will produce a file of recs not inc those in the to-be-deleted list. |
Quote:
I would mention that even with what you have you could submit only a single line for each coloured bracket you have: Code:
while( $dotline = <DOT> ) { |
All times are GMT -5. The time now is 02:18 AM. |