hi there,
Thanks a lot for the help. there are numerous options to solve
my problem. Appreciate all the help.
Special thanks to all who reponded to my questions. Summary is attached below.
Rakesh
---------------------------------------------------------------------
From: Geert Jan Bex <gjb_at_luc.ac.be>
#!/usr/local/bin/perl
$word = 'word to search';
# suppose input is stdin, output is stdout, use shell redirection if required
while(<>){
chop($_);
if(!/$word/){
print "$_\n";
}
}
# the end
From: Dave McFerren <davem_at_solve.net>
What I do is to read the entire text file into a buffer (an associative array), and print out the line to the file IF it does not match the search string.
Hope this helps...
Lots of examples in the Programming Perl book from O'Rielly.
From: Kurt Knochner <Kurt_Knochner_at_Physik.TU-Muenchen.DE>
perl -pi -e 's/.*\n// if /search-string/' file
Regards
Kurt
From: Christophe Wolfhugel <wolf_at_pasteur.fr>
Would s/^.*your_word.*$//; do the job ?
From: kimball_at_falcon.invincible.com
Lines can't be deleted from a plain text file...
you must recreate the file.
-----
This question isn't really in the scope of this mailing list -- you may
get a nastygram from the group owner on this point -- but I'll tell you
anyway:
#!/usr/local/bin/perl
_at_lines=3D<>;
print grep(!/matching regexp/,_at_lines);
Robert L. McMillin | Not the voice of Syseca, Inc. | rlm_at_syseca-us.com
Personal: rlm_at_helen.surfcty.com |=A0rlm_at_netcom.com
------
From: Tom Webster <webster_at_ssdpdc.mdc.com>
Think of it the other way around, you only want lines that don't match your
regular expression to be copied to the final version of the file.
>From that point on there are a number of ways to go about doing this,
the simple (and safe) way is to use a temporary file. If you want to do
things the 'manly' way, you should be able to do the above as a single line
command by editing the file in place.
In any event, here is what the code might look like (I'm typing this off the
top of my head, so it might not be right -- test on a scratch file first):
----- snip ----- snip ----- snip ----- snip ----- snip -----
#!/usr/local/bin/perl
# This script will take multiple files off of the command line
while(<>) {
# See if we are on a different file now
if ($ARGV ne $oldargv) {
# Rename the current (open) file to filename.bak
rename($ARGV, $ARGV.'.bak');
# Open the old filename as an output file
open(ARGVOUT, ">$ARGV");
# Make the new file the default for print output
select(ARGVOUT);
# Store the current filename for future comparison
$oldargv = $ARGV;
}
} continue {
# The continue block is to handle EOF when switching files
# Substitute your word or regex for shazam
if (!/shazam/) {
print;
}
}
# EOF
----- snip ----- snip ----- snip ----- snip ----- snip -----
From: jafar_at_finance.capital.ge.com (Jafar Shameem)
#!/usr/local/bin/perl
$file="your/file/name/here";
$ofile="new/file/name/here";
$pattern="word which u dont want in new file";
open (FILE,$file) or die " $file: $! \n";
open (OFILE,">$ofile") or die " $ofile: $! \n";
while (<FILE>) {
print OFILE if ($_ !~ /$pattern/ );
}
close (FILE);
close (OFILE);
##############################################################################
this is one way of doing it.
simpler way would be on your unix prompt, type:
$ grep -v word file > newfile
From: Brian Sherwood <sherwood_at_esu.edu>
wouldn't "grep -v" be easier?
From: "Feeney, Tim" <Tim.Feeney_at_FMR.COM>
cat $file|grep -v "search string" >$file.tmp
mv $file.tmp $file
From: Cagri Yucel <cyucel_at_is.ku.edu.tr>
I know this is not what you are asking but, if the file is consists of
only one line, why don't you delete the file itself ?
-cagri
From: "Pam Woods, Systems Manager" <axsymgr_at_UAA.ALASKA.EDU>
hmm..
You could just open a new output file
and do an if not found then write record
whenever it was found it wouldn't do the write.
From: "Henry A. Flogel" <hflogel_at_wpl.com>
#!/pathtoperl/perl
$INFILE="filetobesearchedpathname";
$TMPFILE="/tmp/outfiletmp";
open (INFILE "<$INFILE") || die "cannot open input file";
open (TMPFILE ">TMPFILE") || die "cannot open temp file";
while (<INFILE>) {
next if (/searchpattern/);
print TMPFILE $_;
}
close (INFILE) || die "cannot close input file";
close (TMPFILE) || die "cannot close temp file";
rename $TMPFILE $INFILE;
exit;
From: <Phil_Thomas_at_ferntree.com.au>
#!/usr/bin/perl
while (<>) {
print unless /your-word/;
}
...will print all the lines from its input files, unless they
contain the regular expression "your-word".
From: Tom Mornini <tmornini_at_infomania.com>
sed might be better for this sort of thing, or awk, but in Perl,
#!/usr/bin/perl
while (<>)
{
print unless (/word_to_match/);
}
This will work as a pipe, i.e.
theprogramabove.pl <inputfile >outputfile
P.S. Perl has some one like capability, including something called
edit-in-place which might make this more elegant.
From: Rasana Atreya <atreya_at_library.ucsf.edu>
I'm just beginning to teach myself Perl, so this might not be a fancy
solution, but it works:
#!/local/sparc/gnu/bin/perl
# Open file for input
open(IN, "input") || die "can't open input";
# Open file for output
open(OUT, ">output") || die "can't open output";
# Read the single line input into this variable. For multi-line inputs, you
# might want to use an array.
$var = <IN>;
# Check for presence of string "abc"
if ($result = $var =~ /abc/)
{
print OUT "";
}
else
{
print OUT "$var";
}
close(IN);
close(OUT);
rename (output, input);
From: Michael Slattery <slattery_at_angel.net>
Something like this should do it, it is quick and dirty but
serviceable:
First read the file into an array:
open (FILE, "/path/to/file");
_at_lines_in_file = <FILE>;
close(FILE);
Second, write to that file all you have fed into the array,
substituting away the line you don't want.
open (FILE ">/path/to/file");
foreach $line(_at_lines_in_file){
if ($line =~ /$pattern_to_match/){
$line =~ s/^.*$//; #may need better regex!
print FILE "$line";
} else {
print FILE "$line";
}
}
From: Reto Lichtensteiger <rali_at_meitca.com>
It's not a perl specific problem, so I'll pseudocode the algorithm:
create temp file
open temp
open original
while read line
do
if search for match == false
write line to temp file
done
move temp to original
Under unix there is no way to explicitly shrink a file, so you do your
operations and then move files around
From: Vincent Shan <vshan_at_sprint.net>
The easiest way to do it is to read the file and print
the line if it doesn't contain the search word:
while(<>){
print if ! /search_word/;
}
From: mattb_at_deakin.edu.au
The best known way to do this is to read the file in and use the read-buffer to
check for the existance of the string. If it does appear in there you do not wr
ite it out. (Very similar to how you achieve the same result in C.)
A good source for such examples are the O'Reilly books 'Learning Perl' and
'Programming in Perl'.
You may be able to ftp the examples from
ftp://ftp.ora.com/
From: Luca Pizzinato <Pizzinato_at_eumetsat.de>
I read my mail only now, and I think that in the meantime
you already received billions of answers. Anyway...
I would do something like
#!/bin/perl
$word = "someword" ;
open(FILE_IN, "file_in.txt") ;
open(FILE_OUT, ">file_out.txt") ;
while (<FILE_IN>) {
print FILE_OUT unless /\b$word\b/ ;
}
The \b, as you know, are the word boundaries, so you'll
not match words like "blablasomeword" or
"somewordblabla".
From: Reto Lichtensteiger <rali_at_meitca.com>
It's not a perl specific problem, so I'll pseudocode the algorithm:
create temp file
open temp
open original
while read line
do
if search for match == false
write line to temp file
done
move temp to original
Under unix there is no way to explicitly shrink a file, so you do your
operations and then move files around
Reto
--
Received on Thu Jan 23 1997 - 23:11:18 NZDT