How to add a line break before and after a regex in a text file? -



How to add a line break before and after a regex in a text file? -

this excerpt file want edit:

>chr1|-|9|s|somatic accacagccctgttttacgttgcgtcatcgccccgggtgcctggtgacgtcaccagcccgctcg >chr1|+|9|y|somatic accacagccctgttttacgttgcgtcatcgccccgggtgcctggtgacgtcaccagcccgctcg

i new text file in add together line break before ">" , after "somatic" or after "germline", how can in r or unix?

expected output:

>chr1|-|9|s|somatic accacagccctgttttacgttgcgtcatcgccccgggtgcctggtgacgtcaccagcccgctcg >chr1|+|9|y|somatic accacagccctgttttacgttgcgtcatcgccccgggtgcctggtgacgtcaccagcccgctcg

by looks of input, replace spaces newlines:

tr -s ' ' '\n' <infile >outfile

(some tr dialects don't \n. seek '\012' or literal newline: opening quote, newline, closing quote.)

if won't work, can in sed. if somatic static, hard-code it:

sed -e 's/somatic */&\n/g' -e 's/ >/\n>/g' file >newfile

the usual caveats different sed dialects apply. versions don't \n newline, want newline or semicolon instead of multiple -e arguments.

on linux, can modify file in-place:

sed -i 's/somatic */&\ /g s/ >/\ /g' file

(for variation, i'm showing how if sed doesn't recognize \n allows literal newlines, , how set script in single multi-line string.)

on *bsd (including macos) need add together argument -i always; sed -i '' ...

if somatic variable, want replace first space after wedge, seek like

sed 's/\(>[^ ]*\) /\1\n/g'

>[^ ] matches wedge followed 0 or more non-space characters. parentheses capture matched string \1. again, sed variants don't want backslashes in front end of parentheses, or otherwise ... different.

if have long lines, might bump sed has problems that. maybe seek perl instead. (luckily, no dialects worry about!)

perl -i -pe 's/(>[^ ]*) /$1\n/g;s/ >/\n>/g' file

(skip -i alternative if don't want modify input file. output standard output.)

regex text edit

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -