2

Consider the following bash script with a simple regular expression:

for f in "$FILES"
do
  echo $f
  sed -i '/HTTP|RT/d' $f
done

This script shall read every file in the directory specified by FILES and remove the lines with occurrences of 'http' or 'RT' However, it seems that the OR part of the regular expression is not working. That is if I just have sed -i '/HTTP/d' $f then it will remove all lines containing HTTP but I cannot get it to remove both HTTP and RT

  1. What must I change in my regular expression so that lines with HTTP or RT are removed?

Thanks in advance!

3 Answers 3

2

Two ways of doing it (at least):

  1. Having sed understand your regex:

    sed -E -i '/HTTP|RT/d' $f

  2. Specifying each token separately:

    sed -i '/HTTP/d;/RT/d' $f

Sign up to request clarification or add additional context in comments.

2 Comments

I tried man sed to look for the -E option. Obviously since this solution worked, I can see that it makes sed recognize my regex, but can you show me where to find the documentation?
-E is actually a synonym for -r (which would've also worked). -E is in there for [BSD backwards compatibility][1], and also what man sed on my OSX suggested. [1]: blog.dmitryleskov.com/small-hacks/mysterious-gnu-sed-option-e
1

Before you do anything, run with the opposite, and PRINT what you plan to DELETE:

sed -n -e '/HTTP/p' -e '/RT/p' $f

Just to be sure you are deleting only what you want to delete before actually changing the files.

"It's not a question of whether you are paranoid or not, but whether you are paranoid ENOUGH."

1 Comment

hahaha I like the paranoid quote. However, I am already one step ahead as I have backup files :)
0

Well, first of all, it will process all WORDS in the FILES variable.

If you want it to do all files in the FILES directory, then you need something like this:

for f in $( find $FILES -maxdepth 1 -type f )
do
   echo $f
    sed -i -e '/HTTP/d' -e '/RT/d' $f
done

You just need two "-e" options to sed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.