我是编程新手awk
。我有一个关于操作文本文件的问题,需要在可视化软件(Circoshttp://circos.ca)
我有输入数据,我想使用 来处理其值awk/grep/sed
。共有 9 对(18 行)。5 对(前 10 行)用于“from=ABCB11”,4 对(接下来的 8 行)用于“from =ABCC8”。我想要从第一对的第一行中提取值,并将其替换到其余对的每个交替行中。因此,group-2 的值为 9 10,它应该替换 group2 中出现的所有值。group-2 的下一个值是 28 29,应该替换为 9 10。
停靠点应由“from=name”确定,即“from=ABCB11”。必须从下一次出现中捕获表达式并替换的行不必像本例中那样属于组 2。它可以是第 3 组或第 4 组,直到第 10 组。因此第二组(“来自 =ABCC8”)可能属于第 4/5/6 组,而不一定属于第 2 组。这只是一个巧合,但对于每个“from =”备用行,所有组编号都相同。
group-2 9 10 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-3 0 1 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-2 28 29 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM1,toid=114,use=1,z=1
group-5 0 1 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM1,toid=114,use=1,z=1
group-2 29 30 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM2,toid=115,use=1,z=1
group-5 1 2 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM2,toid=115,use=1,z=1
group-2 10 11 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=DRD2,toid=158,use=1,z=1
group-3 1 2 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=DRD2,toid=158,use=1,z=1
group-2 11 12 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=EGF,toid=164,use=1,z=1
group-3 2 3 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=EGF,toid=164,use=1,z=1
group-2 21 22 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-3 12 13 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-2 0 1 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1A,toid=21,use=1,z=1
group-1 0 1 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1A,toid=21,use=1,z=1
group-2 1 2 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1B,toid=22,use=1,z=1
group-1 1 2 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1B,toid=22,use=1,z=1
group-2 2 3 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1D,toid=23,use=1,z=1
group-1 2 3 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1D,toid=23,use=1,z=1
以下是我正在寻找的最终输出:
group-2 9 10 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-3 0 1 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-2 9 10 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM1,toid=114,use=1,z=1
group-5 0 1 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM1,toid=114,use=1,z=1
group-2 9 10 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM2,toid=115,use=1,z=1
group-5 1 2 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=CHRM2,toid=115,use=1,z=1
group-2 9 10 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=DRD2,toid=158,use=1,z=1
group-3 1 2 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=DRD2,toid=158,use=1,z=1
group-2 9 10 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=EGF,toid=164,use=1,z=1
group-3 2 3 text color=black,from=ABCB11,fromid=4,order=2,thickness=3,to=EGF,toid=164,use=1,z=1
group-2 21 22 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-3 12 13 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ACE,toid=11,use=1,z=1
group-2 21 22 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1A,toid=21,use=1,z=1
group-1 0 1 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1A,toid=21,use=1,z=1
group-2 21 22 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1B,toid=22,use=1,z=1
group-1 1 2 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1B,toid=22,use=1,z=1
group-2 21 22 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1D,toid=23,use=1,z=1
group-1 2 3 text color=black,from=ABCC8,fromid=5,order=2,thickness=3,to=ADRA1D,toid=23,use=1,z=1
另外,这只是一个示例数据。如此多的配对将有第 1 组、第 4 组、第 5 组直至第 10 组。这里仅提及较小组的配对。
基本上,这些是在打印完整行时应该看到的修改,直到每个备用行中的“from=ABCB11”结束。对于“from=ABCC8”也是如此。
group-2 9 10
group-3 0 1
group-2 9 10
group-5 0 1
group-2 9 10
.........
.........
.........
这是我尝试过的:
我想循环遍历这些行,直到“from = name”中的值保持不变,以便我可以更改每个备用行中的所有出现次数。代码:
awk -F, 'NR%2==1 {split($2,a,"="); print a[2]}' file.txt
上面的代码能够提取“from=name”中的替代行和“name”。
答案1
尝试类似的方法:
awk 'NR%2{if(p!=$2){v=$1; p=$2}$1=v}1' FS=, OFS=, file
这测试是否$2
已更改并$1
在每个奇数行上用相同的值替换每个奇数行$2
添加了解释..
awk '
NR%2 { # On every line where NR (recordnr) mod 2 equals 1 (odd line)
if(p!=$2) { # If the variable p (previous) is different from the 2nd field then
v=$1 # The new value v should become $1 (1st field)
p=$2 # The new variable p becomes the current field 2
} #
$1=v # On every odd line field 1 becomes the previously set value
}
1 # 1 means true. The default action is to print the record (line)
' FS=, OFS=, file # Set the input and output field separators to a comma.