kipper3d
asked on
Removing a line from a file.
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.
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.
12122:someuser_2:domain_2.
12123:someuser_3:domain_3.
12124:someuser_4:domain_4.
12125:someuser_5:domain_5.
12126:someuser_6:domain_6.
12127:someuser_7:domain_7.
12128:someuser_8:domain_8.
12129:someuser_9:domain_9.
The only information we have is domain_5.com so we need to remove that entire line containing that domain.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks mjcoyne,
This wasn't what i had before but it works - so thanks!
This wasn't what i had before but it works - so thanks!
#!/usr/bin/perl
local $^I=".bak";
local @ARGV=("dbfile.txt");
while( <> ){
print unless /:domain_5\./;
}
local $^I=".bak";
local @ARGV=("dbfile.txt");
while( <> ){
print unless /:domain_5\./;
}
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\./);
}