在每个字符处分割字符串

时间:2014-06-30 11:42:47

标签: r perl

我想拆分字符串中的每个字符,并将其输出为逗号或制表符分隔的字符: 我需要使用file_infile_out因为我有很多行。

输入

TTTGGC
TTTG
TGCAATGG
....
....

输出

T,T,T,G,G,C
T,T,T,G
T,G,C,A,A,T,G,G

我已经使用了它,但它会垂直打印每个字符:

  /usr/bin/perl
   use strict;
   use warnings;

    my $data = 'Becky Alcorn';

   my @values = split(undef,$data);

  foreach my $val (@values) {
   print "$val\n";
  }

  exit 0;

7 个答案:

答案 0 :(得分:3)

您的代码使用循环来打印每行@values一个值,因此计算机会执行您告诉它的操作。尝试:

print join ",", @values;

甚至将代码压缩到:

print join ",", split //, $data;

答案 1 :(得分:3)

使用perl one-liner,

perl -F -lane 'print join ",", @F' file

输出

T,T,T,G,G,C
T,T,T,G
T,G,C,A,A,T,G,G

答案 2 :(得分:2)

在R中,您可以使用strsplitpaste

Strings <- c("TTTGGC","TTTG","TGCAATGG")
vapply(strsplit(Strings, ""), function(x) paste(x, collapse=","), character(1L))
# [1] "T,T,T,G,G,C"     "T,T,T,G"         "T,G,C,A,A,T,G,G"

您可以使用writeLines编写输出,如果需要,请指定sep = "\n"

答案 3 :(得分:1)

awk one-liner:

awk -v FS='' -v OFS="," '$1=$1' file

答案 4 :(得分:1)

open my $in, '<','in.txt';
open my $out, '>', 'out.txt';

while(<$in>){
    chomp;
    my @split = split(//);
    print $out join ",", @split;
    print $out "\n";
}

答案 5 :(得分:0)

这是一种使用基本R功能的快速简便方法。 paste的文档实际上指出paste可以用作&#34;相反的&#34; strsplit

paste0 collapse = ","在这里很不错

> txt <- c("TTTGGC", "TTTG", "TGCAATGG")  ## your data
> sapply(strsplit(txt, ""), paste0, collapse = ",")
# [1] "T,T,T,G,G,C"     "T,T,T,G"         "T,G,C,A,A,T,G,G"

答案 6 :(得分:0)

或使用stringr包:

txt <- c("TTTGGC", "TTTG", "TGCAATGG") 
unlist(str_split(string=txt, pattern= ""))
 [1] ""  "T" "T" "T" "G" "G" "C" ""  "T" "T" "T" "G" ""  "T" "G" "C" "A" "A" "T" "G" "G"