awk - Introduce a new line before every row containing a specific word in linux -
i new linux. have tab delim text file following
a1 title body.1 gene a1 head head.1 head a1 trunk trunk.1 trunk a1 tail tail.1 tail a2 title body.2 gene a2 head head.2 head a2 trunk trunk.2 trunk a2 tail tail.2 tail a3 title body.3 gene a3 head head.3 head a3 trunk trunk.3 trunk a4 title title.4 gene a4 trunk trunk.4 trunk a4 tail tail.4 tail
i introduce new line before every row containing word "gene" in last column following:
a1 title body.1 gene a1 head head.1 head a1 trunk trunk.1 trunk a1 tail tail.1 tail a2 title body.2 gene a2 head head.2 head a2 trunk trunk.2 trunk a2 tail tail.2 tail a3 title body.3 gene a3 head head.3 head a3 trunk trunk.3 trunk a4 title title.4 gene a4 trunk trunk.4 trunk a4 tail tail.4 tail
i tried following command
sed 's/gene/\ \n&\g' file.txt
but introduces new line after row containing word "gene".
it great if 1 guide me how introduce new line before row containing word "gene
" in last column.
you want (extended regex syntax):
$ sed -r 's/(^.*?\tgene$)/\n\1/' example a1 title body.1 gene a1 head head.1 head a1 trunk trunk.1 trunk a1 tail tail.1 tail a2 title body.2 gene a2 head head.2 head a2 trunk trunk.2 trunk a2 tail tail.2 tail a3 title body.3 gene a3 head head.3 head a3 trunk trunk.3 trunk a4 title title.4 gene a4 trunk trunk.4 trunk a4 tail tail.4 tail
in regex can see:
- a substitution command
's/.../.../'
- capture group of whole line ends tab character , gene:
(^.*?\tgene$)
. - inserting newline character , captured group (first , only) result:
\n\1
please note there catch in question:
i introduce new line before every row containing word "gene" in last column
this leads assumption need result's first line empty (or single newline precise)
however example's first line not preceded empty line. if need, should use sed addressing:
pono@pono-carbon:~$ sed -r '2,$s/(^.*?\tgene$)/\n\1/' example a1 title body.1 gene a1 head head.1 head a1 trunk trunk.1 trunk a1 tail tail.1 tail a2 title body.2 gene a2 head head.2 head a2 trunk trunk.2 trunk a2 tail tail.2 tail a3 title body.3 gene a3 head head.3 head a3 trunk trunk.3 trunk a4 title title.4 gene a4 trunk trunk.4 trunk a4 tail tail.4 tail
Comments
Post a Comment