1

I would like to delete all lines containing a certain string, as well as the next 3 lines.

I can use the sed command to do this easily when using a single string:

sed '/HISEQ:243:C9FH7ANXX:4:2202:4922:44902/,+3 d' ../input/infile.fq_1 > ../output/outfile.fq_1

The double quoted version also works:

sed "/HISEQ:243:C9FH7ANXX:4:2202:4922:44902/,+3 d" ../input/infile.fq_1 > ../output/outfile.fq_1

However, I would like to use many strings. Each string is a line in a text file named strings.txt, and I would like to match one string at a time, removing the lines that match in the infile as well as the next 3 each time, and writing the output to the outfile.

So I've tried:

cat strings.txt | while read LINE
do
sed '/$LINE/,+3 d' ../input/infile.fq_1 > ../output/outfile.fq_1
done

But the variable isn't recognized with single quotes. If I use double quotes:

cat strings.txt | while read LINE
do
sed "/$LINE/,+3 d" ../input/infile.fq_1 > ../output/outfile.fq_1
done

This doesn't work either.

I've also tried all kinds of variations, like using curly brackets or using both types of quotes:

cat strings.txt | while read LINE
do
sed '/'"$LINE"'/,+3 d' ../input/infile.fq_1 > ../output/outfile.fq_1
done

But nothing seems to work. Help would be much appreciated!

  • 1
    The last two should work, provided that $LINE doesn't contain any regex metacharacters or additional / delimiters. What happens exactly (no lines get deleted? too many lines get deleted?). Can you give us a minimal working example (strings.txt and infile.fq_1 files)? – steeldriver Aug 29 '16 at 18:31
0

The problem here is that you keep re-writing your output file each time through the loop. Try this:

#!/bin/bash

cp ../input/infile.fg_1 temp

while read line; do
   sed -i "/$line/,+3 d" temp
done < strings.txt

mv temp ../output/outfile.fg_1

The -i in the sed command ADDS each change to the temp file and then once all of the changes have been made, we move it to your output file. If you wanted to work directly on the output file then do this:

#!/bin/bash

cp ../input/infile.fg_1 ../output/outfile.fg_1

while read line; do
   sed -i "/$line/,+3 d" ../output/outfile.fg_1
done < strings.txt

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.