0

I have a fastq file where I must delete everything in certain lines except for the first character. Here is an example:

@A202BNABXX:2:1:4523:2219/1
GGCCGAATTCACCGATAAAATACACAGACGGTAAATGTTCCCACTAGGTTCACCTATGAGTCCGCTATGCCCGCCGGCATTTCAATTTCT
+ERR706845.1.1 A202BNABXX:2:1:4523:2219 length=90
@A202BNABXX:2:1:4715:2246/1
CTGAGGACTGCATGACATGGACTGATGCACTCTATAATCATTTTAATGCAATTGTCACTATTCATGAAATTGTGTAACTTACTAAATAGT
+ERR706845.2.1 A202BNABXX:2:1:4715:2246 length=90

The plus is at the beginning of the line in the fastq file. For every line that starts with the + symbol, I need to delete everything after it. Then I need to rewrite the output to a new file. Any suggestions would be greatly appreciated. Thanks so much!

1 Answer 1

0

Using sed

$ sed -E 's/^(\+).*/\1/' input_file > new_file
$ cat new_file
@A202BNABXX:2:1:4523:2219/1
GGCCGAATTCACCGATAAAATACACAGACGGTAAATGTTCCCACTAGGTTCACCTATGAGTCCGCTATGCCCGCCGGCATTTCAATTTCT

+

@A202BNABXX:2:1:4715:2246/1
CTGAGGACTGCATGACATGGACTGATGCACTCTATAATCATTTTAATGCAATTGTCACTATTCATGAAATTGTGTAACTTACTAAATAGT

+
10
  • 3
    Or just sed 's/^+.*/+/' shorter and more portable. Commented Jan 10, 2023 at 6:36
  • Or sed '/^+/c+' with GNU sed.
    – Kusalananda
    Commented Jan 10, 2023 at 7:19
  • Hello! So the first sed command listed worked! The only problem is that some of my lines that I do not want to delete also start with a +. The lines that I for sure want to delete all start with "+ERR706845". Is there a way to manipulate the sed command so it only deletes everything except the + character on those lines? Thanks!
    – Lydia
    Commented Jan 11, 2023 at 23:03
  • @Lydia Try sed -E '/+ERR706845/s/^(\+).*/\1/'
    – sseLtaH
    Commented Jan 11, 2023 at 23:23
  • Hello @HatLess this is the error I got from running this: sed -E '/+ERR706845/s/^(\+).*/\1/' ERR351507_pass_1.fastq > TR01_R1_001.fastq sed: 1: "/+ERR706845/s/^(\+).*/\1/": RE error: repetition-operator operand invalid thanks so much for the help!!
    – Lydia
    Commented Jan 12, 2023 at 1:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .