I want to edit a file called trial.fastq with the following format:

@NAME123.15.1 15 length=151
+NAME123.15.1 15 length=151
@NAME123.14.1 14 length=150
+NAME123.14.1 14 length=150

I want to change the format to this:

@Inst:1:FID:LANE:TILE:15:Y 1:Y:0:NAME123
@Inst:1:FID:LANE:TILE:14:Y 1:Y:0:NAME123

I've tried many formats of sed including:

sed -i -E 's/@(\w+).(\d+).(\d)\s\d+\slength=\d+\n.*/@Inst:1:FID:LANE:TILE:\2:Y \3:Y:0:\1\n/g' trial.fastq > trial_new.fastq

which I found from copying my TextWrangler find and replace grep search. As well as:

sed -e 's/\(@\w\).\(\d..+\).\(\d\)\s\d..*\slength\=\d..*\n.*$(\w..*)\(.*\)$\+.*$.*$/@Inst\:1\:FID\:LANE\:TILE\:\2\:Y \3\:Y\:0\:\1\n\4/g' trial.fastq

I am pretty sure I am going about this the wrong way. Can someone help me understand what I'm doing wrong and how to fix this problem?

  • Note that depending on your sed flavor \d is not a digit. That's a typical pitfall for people used to regexp in other tools – Philippos Mar 7 '17 at 21:40

I hope I understood you desired output correctly!

$ sed -nr '/#/d;s/@([^\.]+)[^ ]+ ([0-9]+) .*/@Inst:1:FID:LANE:TILE:\2:Y 1:Y:0:\1/p;s/^[A-Z]/\+&/p' file
@Inst:1:FID:LANE:TILE:15:Y 1:Y:0:NAME123
@Inst:1:FID:LANE:TILE:14:Y 1:Y:0:NAME123


  • -n don't print until we ask for it
  • -r use ERE
  • /#/d delete lines with #
  • ([^\.]+) save some characters that are not.` for later
  • [^ ]+ ([0-9]+) .* save some numbers between spaces
  • \1 backreference to saved pattern in replacement
  • ; separates commands, like in the shell
  • s/^[A-Z]/\+&/ replace lines that start with a capital letter with themselves prefixed with +
  • p print the lines we worked on

Use the -i flag if you want to edit the file in place, after testing the output is what you want (repeat the command with sed -nri ....)

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.