remove line from text file

anyone know how I could remove a line from a text file

example:

--------------- starts
fee
fi
foe
fum

--------------- bcomes
fee
foe
fum

this text file could become large so if there is a shell command i can run that would be cool though I have some future concern about escaping characters.

thanks for any help

ok…
well. I have an answer with a only a logic road block.

so time to share :slight_smile:


--just setting up some files
set infile to quoted form of POSIX path of ("" & (path to preferences) & "infile.txt")
set outfil to quoted form of POSIX path of ("" & (path to preferences) & "outfil.txt")

-- remove lines containing "foo"
set theRecord to do shell script "grep -iv " & foo " " & infile & " > " & outfile

explaination

grep <— command

-iv <------ i is for case insensative and the v is for reverse result so if i look for “foo” i get every line that DOES NOT contain “foo”

foo <— string we are looking for

infile <----------- file to search in this must exist

“>” says to write the results to the outfile

outfile <--------- this file file can exist but it doesn’t have too

so now for the logic problem

when it gets to the last one there are are no more lines left except the one that contains the last search string. but if there is nothing left i want it to write an empty file or remove it all together but I’m afraid there might be some other reason that it may return a zero result and i don’t want to pitch the file if there is stuff in it that i need

thanks for any help
mm

When using grep -v, if all of the input lines match the pattern grep will produce no output. When coupled with the shell-based output redirection your output file should be empty. That sounds like it fits your requirement of ending up with an empty file.

Or maybe you are concerned that grep might filter out more lines than you expect? If this is the case, you should remember that, by default, grep does its searching/tests based on regular expressions, not plain strings. Among other things, this means that some characters in the search pattern (your foo variable) will have special meaning and that if the pattern matches anywhere on a line that line will be omitted from the output (assuming -v). If this is not what you expect, you may want to use a different filter.

The fgrep program (or the -F option to grep) uses “fixed string” matching (no regular expressions), but by itself, it still allows matches anywhere in the data lines. The -x option can help with the substring matching situation.

grep -v foo would filter out lines like “foo” “Buy some more food.” and “Disabuse the fools of their inappropriate notions.” (matches underlined). A pattern like No. More. would use the special regular expression meaning of the period character and could match a line like “I said ‘Not More spam!’”. You could use fgrep or grep -F to prevent this special interpretation, but that still allows substring matching. grep -Fxv foo would only filter out “foo” but not “Of all the food, they liked the pralines the best.” or “I am such a fool.”.

Chris,

thanks thats very helpful ! at this point I’m happy with what I’m using but if I run into issue I’ll have to re-visit .

mm

Also, the -w option

so if your text file held
fee
fi
foe
fum
find

grep -ivw fi

would return

fee
foe
fum
find