Solved

Removing a line from a file.

Posted on 2007-11-23
4
180 Views
Last Modified: 2012-05-05
I know this question is everywhere on here - but im missing some detail, either due to brain fart or...  I figured this out a few weeks ago and either deleted it or forgot to document it now i can't seem to figure it out again. Argg - thanks for your help!

Heres the one liner:
perl -i -ne 'print unless /EXP/' file

This prints contents minus the line in the match.

My needs:
1. Needs to be in perl script not a one liner.
2. Needs to remove the line from a file permanently
3. There are 3 columns in the file, we only know the content of one portion of the line.

Example data from dbfile.txt:
12121:someuser_1:domain_1.com
12122:someuser_2:domain_2.com
12123:someuser_3:domain_3.com
12124:someuser_4:domain_4.com
12125:someuser_5:domain_5.com
12126:someuser_6:domain_6.com
12127:someuser_7:domain_7.com
12128:someuser_8:domain_8.com
12129:someuser_9:domain_9.com

The only information we have is domain_5.com so we need to remove that entire line containing that domain.

0
Comment
Question by:kipper3d
  • 2
4 Comments
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20340653
#!/usr/bin/perl -w
use strict;

open (IN, ">dbfile.txt") or die "Couldn't open dbfile.txt: $!\n";
open (OUT, ">dbfile-new.txt") or die "Couldn't open dbfile-new.txt: $!\n";

while (<IN>) {
    print OUT unless (/:domain_5\./);
}
0
 
LVL 17

Accepted Solution

by:
mjcoyne earned 50 total points
ID: 20340655
Sorry --  DO NOT use that script.  It'll clobber your dbfile.txt original.  Here's the corrected version:

#!/usr/bin/perl -w
use strict;

open (IN, "dbfile.txt") or die "Couldn't open dbfile.txt: $!\n";
open (OUT, ">dbfile-new.txt") or die "Couldn't open dbfile-new.txt: $!\n";

while (<IN>) {
    print OUT unless (/:domain_5\./);
}
0
 
LVL 1

Author Closing Comment

by:kipper3d
ID: 31410710
Thanks mjcoyne,

This wasn't what i had before but it works - so thanks!
0
 
LVL 84

Expert Comment

by:ozo
ID: 20340875
#!/usr/bin/perl
local $^I=".bak";
local @ARGV=("dbfile.txt");
while( <> ){
   print unless /:domain_5\./;
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question