合并条件为AWK的两行

时间:2016-11-09 12:52:03

标签: awk printing merge uniq

我有疑问。我想将具有条件的两行或三行合并为一行并进行特定打印。

INPUT:文件有6行和制表符分隔

LOL   h/h  2  a  b  c
LOLA  h/h  3  b  b  b
SERP  w/w  4  c  c  c 
DARD  s/s  5  d  d  d 
GIT   w/w  6  a  b  c
GIT   h/h  6  a  a  b
GIT   d/d  6  a  b  b
LOL   h/h  7  a  a  a

输出:有两个条件:if($1 s相同且$3 s相同)将行与特定打印合并

LOL   h/h  2  a  b  c
LOLA  h/h  3  b  b  b
SERP  w/w  4  c  c  c 
DARD  s/s  5  d  d  d 
GIT   w/w  6  a  b  c h/h  6  a  a  b d/d  6  a  b  b
LOL   h/h  7  a  a  a

我有这段代码:

awk -F'\t' -v OFS="\t" 'NF>1{a[$1] = a[$1]"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6};END{for(i in a){print i""a[i]}}'

但它只是在第1列合并,我不确定使用此代码是否合适。

3 个答案:

答案 0 :(得分:1)

在awk中:

"-----BEGIN CERTIFICATE-----\nMIIDHDCCAgSgAwIBAgIIORr4anMsfgIwDQYJKoZIhvcNAQEFBQAwMTEvMC0GA1UE\nAxMmc2VjdXJldG9rZW4uc3lzdGVtLmdzZXJ2aWNlYWNjb3VudC5jb20wHhcNMTYx\nMTA4MDA0NTI2WhcNMTYxMTExMDExNTI2WjAxMS8wLQYDVQQDEyZzZWN1cmV0b2tl\nbi5zeXN0ZW0uZ3NlcnZpY2VhY2NvdW50LmNvbTCCASIwDQYJKoZIhvcNAQEBBQAD\nggEPADCCAQoCggEBANsqepl6/iEsKBsBRxNHC+MKQ77aVlJBL0jQxxAZzeXXTabP\nYX4mHLQHMdDMhFEVpntjykI0Vfi9p//11pIuErwPnx/9gFIUB6fo3aAT+qaAmk+6\nQObPUARv47xTGhvO/KV5zO28qdg4YmhuFlEk5YjzoVIiyfAv0KTy+gxoNnlZmZA1\nUMIkrjnWrmfkNVRn5aiVMewqpT8m1e6bmu57ZzlWcSWbUXQb7DdjG0Vq1irFEq4z\nu7bOJy+NK2uPz3FX4qfOZyXoJpBSHIZ7BQRI36WTZjlrDmgEfTombnv9LtPZWxzd\nIAwB84kc0tVk4QoNH9Pj6Hchit8RGNk0vTdFdDMCAwEAAaM4MDYwDAYDVR0TAQH/\nBAIwADAOBgNVHQ8BAf8EBAMCB4AwFgYDVR0lAQH/BAwwCgYIKwYBBQUHAwIwDQYJ\nKoZIhvcNAQEFBQADggEBAGVqaoK081KqZs5eGpHG2pRsaQhK1/keJp9/BsfMktZ3\ny0n4OrLYIxysH1eBIpCuDCi+vrKR4D/KnFVErQKC94MhpZwL1J8p7eZQOPuYnJT5\nFpYBCA7ZJtEPXxQyUGeRqH/C+B251A0eoFg9LctJv+v+Dp9o2Xd14eG2zuMnycdR\nlKpEB/Tldhxd2eBfIKbI1L8xFQ+ESwhXKtfKo0+B6r6bP4LVUDl32uMT1bBwiSV2\nAgS4qt+YTH2EXVk8fWlHsy2PVLtoRRRGMnKJStrelDI9gW1Sll2obw5OKnqiTHVT\n/blv4sd/fIid/xuABB6XbmFslP0BQxFhiJrtMy66Txc=\n-----END CERTIFICATE-----\n"

说明:

'OpenSSL unable to verify data: error:04091077:rsa routines:INT_RSA_VERIFY:wrong signature length'

答案 1 :(得分:1)

这是支持多维数组的gawk v4的答案。第一个文件中的一列存储在多维数组中,很容易与第二个文件列进行比较。我的解决方案显示了一个示例printf,您可以根据需要进行修改。

#!/bin/gawk -f

NR==FNR {          # for first file
    a[$1][0] = $2; # Store columns in
    a[$1][1] = $3; # multi dimensional
    a[$1][2] = $4; # array
    a[$1][3] = $5;
    a[$1][4] = $6;
    next;
}

$1 in a && $3 == a[$1][1] {
    printf("%s\t%s\n", $2, a[$1,0])
}

答案 2 :(得分:1)

使用gawk v3回答我无法使用多维数组

#!/bin/gawk -f

NR==FNR {
   a[$1]
   b[$1] = $2;
   c[$1] = $3;
   d[$1] = $4;
   e[$1] = $5;
   f[$1] = $6;
   next;
}

$1 in a && $3 == c[$1] {
    print $0
}

<强>一衬垫

gawk 'NR==FNR {a[$1]; b[$1] = $2; c[$1] = $3; d[$1] = $4; e[$1] = $5; f[$1] = $6; next; } $1 in a && $3 == c[$1] { print $0 }' /tmp/file1 /tmp/file2