bash 在每一行的列开头添加字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18061606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add string in each line at the begining of column
提问by Irek
I digged many threads, but neither of them adresses this question as it stands.
我挖掘了很多线索,但他们都没有解决这个问题。
I am interested in addind string chr to the begining of column in each line. File is tab delimited, looks sth like:
我有兴趣将字符串 chr 添加到每行列的开头。文件是制表符分隔的,看起来像:
re1 1 AGT
re2 1 AGT
re3 2 ACGTCA
re12 3 ACGTACT
what I need is:
我需要的是:
re1 chr1 AGT
re2 chr1 AGT
re3 chr2 ACGTCA
re12 chr3 ACGTACT
Can be in bash oneliner
可以在 bash oneliner
many thanks for any help, cheers, Irek
非常感谢您的帮助,干杯,Irek
回答by fedorqui 'SO stop harming'
What about this?
那这个呢?
$ awk '="chr"' file
re1 chr1 AGT
re2 chr1 AGT
re3 chr2 ACGTCA
re12 chr3 ACGTACT
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
.
随着$2="chr"$2
我们添加chr
到第二个字段。然后我们不需要任何其他命令来获得所需的输出,因为 awk 的默认行为是print $0
.
To make sure the OFS (output field separator) is a tab, you can do the following:
要确保 OFS(输出字段分隔符)是选项卡,您可以执行以下操作:
$ awk 'BEGIN{OFS="\t"}="chr"' file
re1 chr1 AGT
re2 chr1 AGT
re3 chr2 ACGTCA
re12 chr3 ACGTACT
回答by MattH
Awk one-liner do?
awk单行吗?
$ awk -v OFS=$'\t' '{ ="chr" ; print}' so.txt
re1 chr1 AGT
re2 chr1 AGT
re3 chr2 ACGTCA
re12 chr3 ACGTACT
回答by mohit
sed
one-liner:
sed
单线:
sed 's/\<[0-9]\>/chr&/' < input > output