sed的新手,试图完成以下操作,但完全卡住了: 我正在尝试在第二列中用sed替换模式。此模式发生了多次。
我有:
Gene1 GO:0000045^biological_process^autophagosome assembly`GO:0005737^cellular_component^cytoplasm
Gene2 GO:0000030^molecular_function^mannosyltransferase activity`GO:0006493^biological_process^protein O-linked glycosylation`GO:0016020^cellular_component^membrane
我想得到:
Gene1 GO:0000045,GO:0005737
Gene2 GO:0000030,GO:0006493,GO:0016020
因此,请删除所有描述性部分,并使用“,”作为分隔符。我选择使用sed是因为我认为可以轻松识别^和`之间的模式。但是,它会删除所有第一个GO条款。
代码:
sed -E 's/(^)'.+'(`)/,/g'
有人可以帮我吗?
答案 0 :(得分:0)
尝试一下,显示为两个步骤
$ # showing how to remove from ^ to ` and replace with ,
$ sed 's/\^[^`]*`/,/g' ip.txt
Gene1 GO:0000045,GO:0005737^cellular_component^cytoplasm
Gene2 GO:0000030,GO:0006493,GO:0016020^cellular_component^membrane
$ # removing remaining data from ^ to end of line as well
$ sed 's/\^[^`]*`/,/g; s/\^.*//' ip.txt
Gene1 GO:0000045,GO:0005737
Gene2 GO:0000030,GO:0006493,GO:0016020
^
是一个元字符,请使用\^
从字面上进行匹配[^`]*
将匹配零个或多个非`
字符\^.*`
,由于greedy nature of quantifiers,这将从行的第一个^删除到最后一个反引号答案 1 :(得分:0)
sed -e 's/\^[^`]*//g' -e 's/`/,/g' your_file
第一个命令删除(除以`
之后^
之后的任何字符(不包括任何字符)
用`
替换,
答案 2 :(得分:0)
识别单个字段,然后对每个字段进行操作,可能比仅使用正则表达式识别每一行的部分更为有用:
public interface IScheduleClientContract
{
void UpdateToCheckedIn(string id);
}
[Fact]
public void UpdateToCheckedIn_Should_Broadcast_Id()
{
var hub = new ScheduleHub();
var clients = new Mock<IHubCallerConnectionContext<dynamic>>();
var all = new Mock<IScheduleClientContract>();
hub.Clients = clients.Object;
all.Setup(m=>m.UpdateToCheckedIn(It.IsAny<string>())).Verifiable();
clients.Setup(m => m.All).Returns(all.Object);
hub.UpdateToCheckedIn("id");
all.VerifyAll();
}
。
$ awk -F'^' -v OFS=',' '{print NR") "$0; for (i=1;i<=NF;i++) print "\t"i") "$i}' file
1) Gene1 GO:0000045^biological_process^autophagosome assembly`GO:0005737^cellular_component^cytoplasm
1) Gene1 GO:0000045
2) biological_process
3) autophagosome assembly`GO:0005737
4) cellular_component
5) cytoplasm
2) Gene2 GO:0000030^molecular_function^mannosyltransferase activity`GO:0006493^biological_process^protein O-linked glycosylation`GO:0016020^cellular_component^membrane
1) Gene2 GO:0000030
2) molecular_function
3) mannosyltransferase activity`GO:0006493
4) biological_process
5) protein O-linked glycosylation`GO:0016020
6) cellular_component
7) membrane