Bash 在awk中,搜索当前行的某些列
我有一个名为Bash 在awk中,搜索当前行的某些列,bash,awk,Bash,Awk,我有一个名为c\u的文件,来自\u V\u 273\u 008245\u 50\u SYMREMO.out,看起来像: NEIGHBORS OF THE NON-EQUIVALENT ATOMS N = NUMBER OF NEIGHBORS AT DISTANCE R ATOM N R/ANG R/AU NEIGHBORS (ATOM LABELS AND CELL INDICES) 1 CA 1 2.4055 4.5458 7 O 0
c\u的文件,来自\u V\u 273\u 008245\u 50\u SYMREMO.out
,看起来像:
NEIGHBORS OF THE NON-EQUIVALENT ATOMS
N = NUMBER OF NEIGHBORS AT DISTANCE R
ATOM N R/ANG R/AU NEIGHBORS (ATOM LABELS AND CELL INDICES)
1 CA 1 2.4055 4.5458 7 O 0 0 0
1 CA 1 2.4058 4.5463 10 O 0-1 0
1 CA 1 2.4356 4.6026 14 O 0 0 0
.
.
.
如果我想在R/ANG
中搜索1ca7o
的距离,它应该是2.4055
我创建了以下脚本:搜索距离。awk:
{if ($0 ~ "NEIGHBORS OF THE NON-EQUIVALENT ATOMS") {FLAG=1}};
# If the current line of the file begins with that string, we asign it a FLAG=1
{if (FLAG==1)
{if ($0 ~ "^ 1 CA"){LINE=$0;
exit}
}
};
# Here I am searching for "1 CA" on each line
END{VOL=FILENAME;
# The filename is: "c_FROM_V_273_008245_50_neighbours_SYMREMO.out"
# My intention is to end up with a new file with 2 columns:
# "volume" and "distance".
# Notice that the filename contains the volume: 273.008245
gsub("^.*_V_","",VOL);
gsub("_",".",VOL);
gsub(".50.neighbours.SYMREMO.out"," ",VOL);
# Some substitutions to make "c_FROM_V_273_008245_50_neighbours_SYMREMO.out"
# to be "273.008245"
# Up to now the output of running:
# search_for_distance.awk -f c_FROM_V_273_008245_50_neighbours_SYMREMO.out
# is the following:
# 273.008245 1 CA 1 2.4055 4.5458 7 O 0 0 0
# So, I need to take LINE and only extract column "4".
# This is done by a "split" command:
{split(LINE,array," ")}
print VOL,array[4]}
运行的输出:
搜索\u distance.awk-f c\u FROM\u V\u 273\u 008245\u 50\u SYMREMO.out
详情如下:
273.008245 2.4055
请注意,脚本正在打印第一次出现的1ca
,它恰好是1ca7o
,这正是我想要的
但是现在我需要运行这个来搜索很多距离的第一个外观
我想搜索1ca
14o
距离的首次出现。
我只需将我正在搜索的代码的第一位从行首修改为1ca
:
{if ($0 ~ "NEIGHBORS OF THE NON-EQUIVALENT ATOMS") {FLAG=1}};
# If the current line begins with that string, we asign it a FLAG=1
{if (FLAG==1)
{if ($0 ~ "^ 1 CA"){LINE=$0;
exit}
}
};
我如何引入搜索1ca
14o
的订单
差不多
{if (FLAG==1)
{if ($0 ~ "/1 CA && /14 O"){LINE=$0;
exit}
}
};
非常感谢你的帮助
我想搜索1ca7o的R/ANG距离,在本例中是2.4055
要查找1 CA 14 O的R/Ang,请执行以下操作:
$ awk '$1==1 && $2=="CA" && $6==14 && $7=="O" {print $4}' file
2.4356
工作原理
这将选择四个规定条件为真的行$1==1&&$2==“CA”&&$6==7&&$8==0
对于选定的行,这将打印第四个字段打印$4
$1==1&&$2==“CA”&&$6==14&&$7==0&&flag==1
我想您可以从there@mpez0:我应该在search\u for\u distance.awk脚本中将此命令放在哪里?ThanksIt是线条模式匹配;它在这行的动作之前。谢谢你的回答,但不幸的是,这不是我要问的。。。。我问的是问题的结尾。谢谢again@DavidC. 将7更改为14就可以了。@John1024如果我要在您的命令中找到1ca7o
,或1ca14o
,则必须是$8==“O”
,而不是$8==0
。而且,awk'$1==1&&$2==“CA”&&$6==7&&$8==“O”{print$4}文件
不起作用,为什么?@DavidC。我更新了答案,在第七列中查找大写字母“O”,并为7和14的情况提供解决方案。
$ awk '$1==1 && $2=="CA" && $6==14 && $7=="O" {print $4}' file
2.4356