Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 删除冒号前的所有文本_R_Unix_Replace_Sed_Awk - Fatal编程技术网

R 删除冒号前的所有文本

R 删除冒号前的所有文本,r,unix,replace,sed,awk,R,Unix,Replace,Sed,Awk,我有一个包含一定行数的文件。每一行看起来像这样: awk -F: '{print $2}' /your/file TF_list_to_test10004/Nus_k0.345_t0.1_e0.1.形容词:PKMYT1 我想删除所有之前的“:”字符,以便只保留作为基因名称的PKMYT1。 由于我不是正则表达式脚本方面的专家,有谁能帮助我使用Unix(sed或awk)或R实现这一点吗?一个与gsub()一起使用的简单正则表达式: x在R中有两种方法: foo <- "TF_list_to

我有一个包含一定行数的文件。每一行看起来像这样:

awk -F: '{print $2}' /your/file
TF_list_to_test10004/Nus_k0.345_t0.1_e0.1.形容词:PKMYT1
我想删除所有之前的“:”字符,以便只保留作为基因名称的PKMYT1。
由于我不是正则表达式脚本方面的专家,有谁能帮助我使用Unix(sed或awk)或R实现这一点吗?

一个与
gsub()一起使用的简单正则表达式:


x在R中有两种方法:

foo <- "TF_list_to_test10004/Nus_k0.345_t0.1_e0.1.adj:PKMYT1"

# Remove all before and up to ":":
gsub(".*:","",foo)

# Extract everything behind ":":
regmatches(foo,gregexpr("(?<=:).*",foo,perl=TRUE))
foo使用sed:

sed 's/.*://' < your_input_file > output_file
它将匹配任何非冒号的内容,后跟一个冒号,并替换为零

请注意,对于这两种模式,它们将在每一行的第一次匹配时停止。如果要对行上的每个匹配项进行替换,请将该选项添加到命令的末尾

还请注意,在linux上(但不是在OSX上),您可以使用
-i
就地编辑文件,例如:

sed -i 's/.*://' your_file

您可以像这样使用
awk

awk -F: '{print $2}' /your/file

在R中肯定有两种以上的方法。这里是另一种

unlist(lapply(strsplit(foo, ':', fixed = TRUE), '[', 2))

如果字符串长度恒定,我想
substr
将比这个或regex方法快。

如果您有GNU
coreutils
可用,请使用
cut

cut -d: -f2 infile

以下是两种等效解决方案:

第一个使用perl的
-a
自动拆分功能,使用
将每行拆分为字段,填充
F
字段数组,并打印第二个字段
$F[1]
(从字段0开始计数)

第二个使用正则表达式将
s//
^
行的开头替换为
*:
任何以冒号结尾的字符,而不包含任何内容

perl -pe 's/^.*://' file

我正在研究一个类似的问题。约翰和乔希·奥布莱恩的建议奏效了。我从这个tibble开始:

library(dplyr)
my_tibble <- tibble(Col1=c("ABC:Content","BCDE:MoreContent","FG:Conent:with:colons"))
我需要创建这个tibble:

  | Col1                  | Col2 | Col3 
1 | ABC:Content           | ABC  | Content 
2 | BCDE:MoreContent      | BCDE | MoreContent 
3 | FG:Content:with:colons| FG   | Content:with:colons
并使用此代码(R版本3.4.2)实现了这一点


my_tibble2在Sacha Epskamp的最佳响应中,我错过了一个非常简单的动作,那就是使用子函数,在本例中,使用“:”(而不是删除它)之前的所有内容,因此非常简单:

foo <- "TF_list_to_test10004/Nus_k0.345_t0.1_e0.1.adj:PKMYT1"

# 1st, as she did to remove all before and up to ":":
gsub(".*:","",foo)

# 2nd, to keep everything before and up to ":": 
gsub(":.*","",foo)

foo我怀疑这可能是给出的最快的R解决方案+1同样,如果任何基因名称本身可能包含一个
,您可以使用
gsub(“^[^:::*:”,“”,foo)匹配并替换第一个
  | Col1 
1 | ABC:Content 
2 | BCDE:MoreContent 
3 | FG:Content:with:colons
  | Col1                  | Col2 | Col3 
1 | ABC:Content           | ABC  | Content 
2 | BCDE:MoreContent      | BCDE | MoreContent 
3 | FG:Content:with:colons| FG   | Content:with:colons
my_tibble2 <- mutate(my_tibble
        ,Col2 = unlist(lapply(strsplit(Col1, ':',fixed = TRUE), '[', 1))
        ,Col3 = gsub("^[^:]*:", "", Col1))
foo <- "TF_list_to_test10004/Nus_k0.345_t0.1_e0.1.adj:PKMYT1"

# 1st, as she did to remove all before and up to ":":
gsub(".*:","",foo)

# 2nd, to keep everything before and up to ":": 
gsub(":.*","",foo)