From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <96076450423389.tru64-unix-managers.marc@archive.invalid> From: jain () smuhep1 ! physics ! smu ! edu (Rakesh Jain) Date: Thu, 23 Jan 1997 21:52:59 +0000 To: tru64-unix-managers Subject: SUMMARY: help with perl script X-MARC-Message: https://marc.info/?l=tru64-unix-managers&m=96076450423389 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 #!/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 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 perl -pi -e 's/.*\n// if /search-string/' file Regards Kurt From: Christophe Wolfhugel Would s/^.*your_word.*$//; do the job ? From: kimball@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 @lines=3D<>; print grep(!/matching regexp/,@lines); Robert L. McMillin | Not the voice of Syseca, Inc. | rlm@syseca-us.com Personal: rlm@helen.surfcty.com |=A0rlm@netcom.com ------ From: Tom Webster 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@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 () { 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 wouldn't "grep -v" be easier? From: "Feeney, Tim" cat $file|grep -v "search string" >$file.tmp mv $file.tmp $file From: Cagri Yucel 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" 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" #!/pathtoperl/perl $INFILE="filetobesearchedpathname"; $TMPFILE="/tmp/outfiletmp"; open (INFILE "<$INFILE") || die "cannot open input file"; open (TMPFILE ">TMPFILE") || die "cannot open temp file"; while () { next if (/searchpattern/); print TMPFILE $_; } close (INFILE) || die "cannot close input file"; close (TMPFILE) || die "cannot close temp file"; rename $TMPFILE $INFILE; exit; From: #!/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 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 outputfile P.S. Perl has some one like capability, including something called edit-in-place which might make this more elegant. From: Rasana Atreya 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 = ; # 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 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"); @lines_in_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(@lines_in_file){ if ($line =~ /$pattern_to_match/){ $line =~ s/^.*$//; #may need better regex! print FILE "$line"; } else { print FILE "$line"; } } From: Reto Lichtensteiger 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 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@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 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 () { 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 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 --