awk:在匹配后删除第一个字段相同的行
(更新的示例和解决方案)数据按第一个字段顺序分组。每个第一个字段的行数是可变的。找到匹配项(非常用关键字)后,需要删除第一个字段与匹配项相同的行 输入为:awk:在匹配后删除第一个字段相同的行,awk,Awk,(更新的示例和解决方案)数据按第一个字段顺序分组。每个第一个字段的行数是可变的。找到匹配项(非常用关键字)后,需要删除第一个字段与匹配项相同的行 输入为: 1 orange dog red 1 apple cat green 2 peach frog grey 3 apple lamb white 3 orange lamb white 3 mango cat yellow 3 apple mouse blue 如果匹配“ca
1 orange dog red
1 apple cat green
2 peach frog grey
3 apple lamb white
3 orange lamb white
3 mango cat yellow
3 apple mouse blue
如果匹配“cat”或“橙色”,则删除具有相同第一个字段(“1”或“3”)的行。输出将是:
2 peach frog grey
解决方案来自Costas:
awk 'NR==FNR{if($0~/cat|orange/)L[$1]=1;next} !($1 in L)' test1.txt test1.txt
awk'
#通过输入1次以查找所有出现的“cat”
NR==FNR{
如果($0~/cat/)
L[$1]=1#将创建的第一个字段添加到数组L中
下一个
}
#如果第一个字段的值不在数组L中,则第二次通过打印行
!($1英寸)的输入
awk'
#通过输入1次以查找所有出现的“cat”
NR==FNR{
如果($0~/cat/)
L[$1]=1#将创建的第一个字段添加到数组L中
下一个
}
#如果第一个字段的值不在数组L中,则第二次通过打印行
!($1英寸)的输入
awk'
#通过输入1次以查找所有出现的“cat”
NR==FNR{
如果($0~/cat/)
L[$1]=1#将创建的第一个字段添加到数组L中
下一个
}
#如果第一个字段的值不在数组L中,则第二次通过打印行
!($1英寸)的输入
awk'
#通过输入1次以查找所有出现的“cat”
NR==FNR{
如果($0~/cat/)
L[$1]=1#将创建的第一个字段添加到数组L中
下一个
}
#如果第一个字段的值不在数组L中,则第二次通过打印行
!($1英寸)的输入
如果非awk答案可以,在perl中,我会这样做:
use strict;
use warnings;
my @disallowed_keywords = qw ( cat orange );
my %records;
my %ids_to_reject;
my $reject_regex = join( "|", @disallowed_keywords );
$reject_regex = qr/$reject_regex/;
while ( my $line = <DATA> ) {
my ($id) = split( ' ', $line );
push( @{ $records{$id} }, $line );
if ( $line =~ $reject_regex ) { $ids_to_reject{$id}++ }
}
foreach my $id ( sort keys %records ) {
if ( not $ids_to_reject{$id} ) {
print join( "\n", @{ $records{$id} } ), "\n";
}
}
__DATA__
1 orange dog red
1 apple cat green
2 peach frog grey
3 orange lamb white
3 mango cat yellow
3 apple mouse blue
使用严格;
使用警告;
my@disallowed_关键字=qw(橙色猫);
我的%记录;
我要拒绝的%ID;
我的$reject_regex=join(“|”,@disallowed_关键字);
$reject_regex=qr/$reject_regex/;
while(我的$line=){
我的($id)=分割(“”,$line);
push(@{$records{$id}},$line);
如果($line=~$reject_regex){$ids_to_reject{$id}++}
}
foreach my$id(排序键%records){
if(不是$ids_到_拒绝{$id}){
打印联接(“\n”,@{$records{$id}),“\n”;
}
}
__数据
1只橙色狗红色
1苹果猫绿
2桃蛙灰
3橙羊肉白
3芒果猫黄
3苹果鼠蓝
如果非awk答案可以,在perl中,我会这样做:
use strict;
use warnings;
my @disallowed_keywords = qw ( cat orange );
my %records;
my %ids_to_reject;
my $reject_regex = join( "|", @disallowed_keywords );
$reject_regex = qr/$reject_regex/;
while ( my $line = <DATA> ) {
my ($id) = split( ' ', $line );
push( @{ $records{$id} }, $line );
if ( $line =~ $reject_regex ) { $ids_to_reject{$id}++ }
}
foreach my $id ( sort keys %records ) {
if ( not $ids_to_reject{$id} ) {
print join( "\n", @{ $records{$id} } ), "\n";
}
}
__DATA__
1 orange dog red
1 apple cat green
2 peach frog grey
3 orange lamb white
3 mango cat yellow
3 apple mouse blue
使用严格;
使用警告;
my@disallowed_关键字=qw(橙色猫);
我的%记录;
我要拒绝的%ID;
我的$reject_regex=join(“|”,@disallowed_关键字);
$reject_regex=qr/$reject_regex/;
while(我的$line=){
我的($id)=分割(“”,$line);
push(@{$records{$id}},$line);
如果($line=~$reject_regex){$ids_to_reject{$id}++}
}
foreach my$id(排序键%records){
if(不是$ids_到_拒绝{$id}){
打印联接(“\n”,@{$records{$id}),“\n”;
}
}
__数据
1只橙色狗红色
1苹果猫绿
2桃蛙灰
3橙羊肉白
3芒果猫黄
3苹果鼠蓝
如果非awk答案可以,在perl中,我会这样做:
use strict;
use warnings;
my @disallowed_keywords = qw ( cat orange );
my %records;
my %ids_to_reject;
my $reject_regex = join( "|", @disallowed_keywords );
$reject_regex = qr/$reject_regex/;
while ( my $line = <DATA> ) {
my ($id) = split( ' ', $line );
push( @{ $records{$id} }, $line );
if ( $line =~ $reject_regex ) { $ids_to_reject{$id}++ }
}
foreach my $id ( sort keys %records ) {
if ( not $ids_to_reject{$id} ) {
print join( "\n", @{ $records{$id} } ), "\n";
}
}
__DATA__
1 orange dog red
1 apple cat green
2 peach frog grey
3 orange lamb white
3 mango cat yellow
3 apple mouse blue
使用严格;
使用警告;
my@disallowed_关键字=qw(橙色猫);
我的%记录;
我要拒绝的%ID;
我的$reject_regex=join(“|”,@disallowed_关键字);
$reject_regex=qr/$reject_regex/;
while(我的$line=){
我的($id)=分割(“”,$line);
push(@{$records{$id}},$line);
如果($line=~$reject_regex){$ids_to_reject{$id}++}
}
foreach my$id(排序键%records){
if(不是$ids_到_拒绝{$id}){
打印联接(“\n”,@{$records{$id}),“\n”;
}
}
__数据
1只橙色狗红色
1苹果猫绿
2桃蛙灰
3橙羊肉白
3芒果猫黄
3苹果鼠蓝
如果非awk答案可以,在perl中,我会这样做:
use strict;
use warnings;
my @disallowed_keywords = qw ( cat orange );
my %records;
my %ids_to_reject;
my $reject_regex = join( "|", @disallowed_keywords );
$reject_regex = qr/$reject_regex/;
while ( my $line = <DATA> ) {
my ($id) = split( ' ', $line );
push( @{ $records{$id} }, $line );
if ( $line =~ $reject_regex ) { $ids_to_reject{$id}++ }
}
foreach my $id ( sort keys %records ) {
if ( not $ids_to_reject{$id} ) {
print join( "\n", @{ $records{$id} } ), "\n";
}
}
__DATA__
1 orange dog red
1 apple cat green
2 peach frog grey
3 orange lamb white
3 mango cat yellow
3 apple mouse blue
使用严格;
使用警告;
my@disallowed_关键字=qw(橙色猫);
我的%记录;
我要拒绝的%ID;
我的$reject_regex=join(“|”,@disallowed_关键字);
$reject_regex=qr/$reject_regex/;
while(我的$line=){
我的($id)=分割(“”,$line);
push(@{$records{$id}},$line);
如果($line=~$reject_regex){$ids_to_reject{$id}++}
}
foreach my$id(排序键%records){
if(不是$ids_到_拒绝{$id}){
打印联接(“\n”,@{$records{$id}),“\n”;
}
}
__数据
1只橙色狗红色
1苹果猫绿
2桃蛙灰
3橙羊肉白
3芒果猫黄
3苹果鼠蓝
如果行与术语匹配,并且词典中未包含第一列,则打印
awk '{
if($0!~/(cat|orange)/){
if(!($1 in f)){
print $0;
}
}else{
f[$1]=1
}
}' input
如果行与术语匹配,并且字典
f
中不包括第一列,则打印
awk '{
if($0!~/(cat|orange)/){
if(!($1 in f)){
print $0;
}
}else{
f[$1]=1
}
}' input
如果行与术语匹配,并且字典
f
中不包括第一列,则打印
awk '{
if($0!~/(cat|orange)/){
if(!($1 in f)){
print $0;
}
}else{
f[$1]=1
}
}' input
如果行与术语匹配,并且字典
f
中不包括第一列,则打印
awk '{
if($0!~/(cat|orange)/){
if(!($1 in f)){
print $0;
}
}else{
f[$1]=1
}
}' input
这看起来像是一个家庭作业问题。到目前为止你试过什么?它必须是awk解决方案吗?看起来您需要“两次传递”数据,因为在读取所有记录之前,您不知道是否发生了匹配。
“3苹果鼠标蓝”
为什么不。。。。因为第一列中的3我想是因为3 mango cat yellow
包含了一个不允许的关键字。Awk是我唯一知道的语言(勉强),我试图只用Awk来完成我的项目。这看起来像是一个家庭作业问题。到目前为止你试过什么?它必须是awk解决方案吗?看起来您需要“两次传递”数据,因为在读取所有记录之前,您不知道是否发生了匹配。“3苹果鼠标蓝”
为什么不。。。。因为第一列中的3我想是因为3 mango cat yellow
包含了一个不允许的关键字。Awk是我唯一知道的语言(勉强),我试图只用Awk来完成我的项目。这看起来像是一个家庭作业问题。到目前为止你试过什么?它必须是awk解决方案吗?