假设我们有以下文件:
John FRIEND_OF Jose
Aspirin INTERACTS_WITH Penicilin
Tree COEXISTS_WITH Grass
Andrew FIEND_OF Jane
John INTERACTS_WITH Paula
INTERACTS_WITH
如果第二个字段匹配或,我需要以相反的顺序打印该行COEXISTS_WITH
。
这是我的 AWK 一句话:
awk '{if ($2 == "INTERACTS_WITH" || $2 == "COEXISTS_WITH") print; print $3, $2, $1; next}1' test.txt
最终输出是:
Jose FRIEND_OF John
Aspirin INTERACTS_WITH Penicilin
Penicilin INTERACTS_WITH Aspirin
Tree COEXISTS_WITH Grass
Grass COEXISTS_WITH Tree
Jane FIEND_OF Andrew
John INTERACTS_WITH Paula
Paula INTERACTS_WITH John
但是,像上面的示例一样单独指定每个条件是很乏味的(特别是如果您有更多条件)。我想知道如何创建一个字符串数组(例如,['INTERACTS_WITH', 'COEXISTS_WITH']
),然后如果第二个字段与数组中的任何元素匹配则打印该行。
答案1
您的示例输入/输出充其量是令人困惑的,因此我不知道您真正想要做什么,但以下是如何将您想要的值指定为字符串,然后对它们执行某些操作:
awk '
BEGIN {
rels = "INTERACTS_WITH COEXISTS_WITH"
split(rels,tmp)
for (i in tmp) {
relSet[tmp[i]]
}
}
{ print }
$2 in relSet {
print $3, $2, $1
}
' file
答案2
我最好带的(可以是一排的)
awk 'BEGIN { r["INTERACTS_WITH"]=1 ; r["COEXISTS_WITH"]=1}
$2 in r {print $3,$2,$1 ; print ; next}
{print}' ...
这几乎不能节省空间。
答案3
完成以下脚本
awk '$2 ~ /INTERACTS_WITH|COEXISTS_WITH/ {$0=$3" "$2" "$1}1' filename
输出
John FRIEND_OF Jose
Penicilin INTERACTS_WITH Aspirin
Grass COEXISTS_WITH Tree
Andrew FIEND_OF Jane
Paula INTERACTS_WITH John
答案4
您可以通过两次print
更改字段顺序来获取它:
{print $1, $2, $3}{print $3, $2, $1}
awk '/INTERACTS_WITH|COEXISTS_WITH/ {print $1, $2, $3}{print $3, $2, $1}' file
Jose FRIEND_OF John
Aspirin INTERACTS_WITH Penicilin
Penicilin INTERACTS_WITH Aspirin
Tree COEXISTS_WITH Grass
Grass COEXISTS_WITH Tree
Jane FRIEND_OF Andrew
John INTERACTS_WITH Paula
Paula INTERACTS_WITH John
或者更简洁地说:
awk '/INTERACTS_WITH|COEXISTS_WITH/ {print $0}{print $3, $2, $1}' file
Jose FRIEND_OF John
Aspirin INTERACTS_WITH Penicilin
Penicilin INTERACTS_WITH Aspirin
Tree COEXISTS_WITH Grass
Grass COEXISTS_WITH Tree
Jane FRIEND_OF Andrew
John INTERACTS_WITH Paula
Paula INTERACTS_WITH John