使用GNU awk (for )的较新版本 (for \s
and\S
而不是[[:space:]]
and ):[^[:space:]]
gensub()
$ cat tst2.awk
BEGIN {re="\\S+\\s+([^:]+):\\s+([^:]+:\\S+)\\s+(\\S+)\\s+(\\S+).*"; FS=OFS=","}
NR==FNR {map[gensub(re,"\\1","")] = gensub(re,"\\2,\\3,\\4",""); next}
{print $0, map[$1]}
$
$ cat file1
007@some.org,007,/Members/Inactive/Delete,2013-06-07T04:41:56.000Z,Never
$
$ cat file2
User 007@some.org: Forward To:None Enabled:false Action:KEEP
$
$ awk -f tst2.awk file2 file1
007@some.org,007,/Members/Inactive/Delete,2013-06-07T04:41:56.000Z,Never,Forward To:None,Enabled:false,Action:KEEP
或使用任何现代 awk:
$ cat tst.awk
BEGIN{ FS=OFS="," }
NR==FNR {
email = $0
gsub(/^[^[:space:]]+[[:space:]]+|:.*/,"",email)
sub(/^[^:]+:[[:space:]]*/,"")
rec = ""
while ( match($0,/[^:]+:[^:[:space:]]+/) > 0 ) {
rec = rec (rec ? OFS : "") substr($0,RSTART,RLENGTH)
$0 = substr($0,RSTART+RLENGTH+1)
sub(/^[[:space:]]+/,"",$0)
}
map[email] = rec
next
}
{ print $0, map[$1] }
$
$ cat file1
007@some.org,007,/Members/Inactive/Delete,2013-06-07T04:41:56.000Z,Never
$
$ cat file2
User 007@some.org: Forward To:None Enabled:false Action:KEEP
$
$ awk -f tst.awk file2 file1
007@some.org,007,/Members/Inactive/Delete,2013-06-07T04:41:56.000Z,Never,Forward To:None,Enabled:false,Action:KEEP