在列的开头在每行中添加字符串 [英] add string in each line at the begining of column

查看:62
本文介绍了在列的开头在每行中添加字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我挖掘了许多线程,但是他们都没有解决这个问题.

I digged many threads, but neither of them adresses this question as it stands.

我对每行中列开头的addind字符串chr感兴趣. 文件用制表符分隔,看起来像:

I am interested in addind string chr to the begining of column in each line. File is tab delimited, looks sth like:

re1 1   AGT
re2 1   AGT
re3 2   ACGTCA
re12    3   ACGTACT

我需要的是:

re1 chr1    AGT
re2 chr1    AGT
re3 chr2    ACGTCA
re12    chr3    ACGTACT

可以在bash oneliner中

Can be in bash oneliner

非常感谢您的帮助, 干杯, Irek

many thanks for any help, cheers, Irek

推荐答案

这怎么办?

$ awk '$2="chr"$2' file
re1 chr1 AGT
re2 chr1 AGT
re3 chr2 ACGTCA
re12 chr3 ACGTACT

说明

对于$2="chr"$2,我们将chr添加到第二个字段.然后,我们不需要任何其他命令即可获得所需的输出,因为awk的默认行为是print $0.

Explanation

With $2="chr"$2 we add chr to the 2nd field. Then we do not need any other command to get the desired output, as the default behaviour of awk is print $0.

要确保OFS(输出字段分隔符)是选项卡,可以执行以下操作:

To make sure the OFS (output field separator) is a tab, you can do the following:

$ awk 'BEGIN{OFS="\t"}$2="chr"$2' file
re1     chr1    AGT
re2     chr1    AGT
re3     chr2    ACGTCA
re12    chr3    ACGTACT

这篇关于在列的开头在每行中添加字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆