Tag Archives: GAIIX

Substitute newline with tab at designated field separator

By yifangt

Hello, I need to replace newline with tab at certain lines of the file (every four lines is a record).

Code:

infile.fq:

@GAIIX-300
ATAGTCAAAT
+
_SZS^\cd
@GAIIX-300
CATACGACAT
+
hhghfdffhh
@GAIIX-300
GACGACGTAT
+
gggfc[hh]f


Code:

outfile:

@GAIIX-300 ATAGTCAAAT + _SZS^\cd
@GAIIX-300 CATACGACAT + hhghfdffhh
@GAIIX-300 GACGACGTAT + gggfc[hh]f


I used

Code:

sed '/^@/N;/^@/N;/^@/N;s/n/t/g' infile.fq


without full understanding. I figured out this oneliner when I tried to understand newline substitution in sed.
1) Can anyone explain the four consecutive /^@/N for me?
2) How can I use another command tr to do the job like:

Code:

!n@ | tr 'n' 't' outfile.tab


by adding the condition !n@ to filter the record separator, which is @ here? I know awk can do the job much easier with RS=”@”, OFS=”t”,

Code:

awk 'BEGIN{RS="@"; OFS="t"} {print $1, $2, $3, $4}' infile.fq


but I want to understand how sed and tr work, if they can, in this case.
Thanks a lot!
YF

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums