简体   繁体   English

通过colum连接两个变量,并用默认值填充空白行

[英]Join two variables by colum and fill the blank row with default value

I want to join two variables by column by column also filling the blank row with some default value. 我想逐列连接两个变量,并用一些默认值填充空白行。

echo var1
location|Serial
001|0
003|1
007|0
009|1

echo var2
name|location|type|built
name1|001|type1|2009
name2|003|type4|2012
name3|007|type1|2010
name4|009|type2|2015
name5|002|type0|2016

I managed to print following: 我设法打印以下内容:

 paste <(echo "$var2"|awk 'BEGIN{print "name","location","type","built","serial"} NR>1') <(echo "$var1"|awk -F'|' 'NR>1{$1=$1;print FS $2}')
name location type built serial |0
name1|001|type1|2009    |1
name2|003|type4|2012    |0
name3|007|type1|2010    |1
name4|009|type2|2015
name5|002|type0|2016

desired output: 所需的输出:

name|location|type|built|serial
name1|001|type1|2009|0
name2|003|type4|2012|1
name3|007|type1|2010|0
name4|009|type2|2015|1
name5|002|type0|2016|NA

Try this - 尝试这个 -

 awk 'BEGIN{FS=OFS="|"} NR==FNR{a[$1]=$2;next} {print (a[$2]!=""?$0 OFS a[$2] : $0 OFS "NA")}' <(echo "$var1") <(echo "$var2")
name|location|type|built|Serial
name1|001|type1|2009|0
name2|003|type4|2012|1
name3|007|type1|2010|0
name4|009|type2|2015|1
name5|002|type0|2016|NA

Explained - 解释-

print (a[$2]!=""?$0 OFS a[$2] : $0 OFS "NA" Working as left outer join, if Value is not found for 5th column it will get replaced with "NA" print(a [$ 2]!=“”?$ 0 OFS a [$ 2]:$ 0 OFS“ NA”用作左外部联接,如果未在第5列中找到Value,它将被替换为“ NA”。

awk 'BEGIN{ 
            FS=OFS="|"                         # set i/p and o/p field separator
     }
     FNR==NR{                                  # Here we read contents from first arg
               a[$1]=$2;                       # populate array a where index being field1 and value being field2
               next                            # stop processing go to next line
     }
     {                                         # Here we read second arg
        print $0,($2 in a) ? a[$2] : "NA"      # if array a has index that is field2 value of 2nd file/var then serial no else NA 
     }' <(echo "$var1") <(echo "$var2")

Since your input is variable not file, so you can do something like below 由于您的输入是变量而不是文件,因此您可以执行以下操作

Define variables 定义变量

$ read -d -r var1 <<EOF                                                             
location|Serial
001|0
003|1
007|0
009|1
EOF

$ read -d -r var2 <<EOF 
name|location|type|built
name1|001|type1|2009
name2|003|type4|2012
name3|007|type1|2010
name4|009|type2|2015
name5|002|type0|2016
EOF  

Contents of variable 变量内容

$ echo "$var1"
location|Serial
001|0
003|1
007|0
009|1

$ echo "$var2"
name|location|type|built
name1|001|type1|2009
name2|003|type4|2012
name3|007|type1|2010
name4|009|type2|2015
name5|002|type0|2016

Output 输出量

$ awk 'BEGIN{FS=OFS="|"}FNR==NR{a[$1]=$2;next}{print $0,($2 in a)?a[$2]:"NA"}' <(echo "$var1") <(echo "$var2")
name|location|type|built|Serial
name1|001|type1|2009|0
name2|003|type4|2012|1
name3|007|type1|2010|0
name4|009|type2|2015|1
name5|002|type0|2016|NA

Read built-in 内建阅读

-d DELIM The first character of DELIM is used to terminate the input line, rather than newline. -d DELIM的第一个字符用于终止输入行,而不是换行符。

-r If this option is given, backslash does not act as an escape character. -r如果给出此选项,则反斜杠不充当转义字符。 The backslash is considered to be part of the line. 反斜杠被认为是该行的一部分。 In particular, a backslash-newline pair may not be used as a line continuation. 特别是,反斜杠-换行符对不能用作行继续。

No need for awk: 无需awk:

paste -d'|' <(<<<"$var2") <(<<<"$var1") | cut --complement -d'|' -f5

Output: 输出:

name|location|type|built|Serial
name1|001|type1|2009|0
name2|003|type4|2012|1
name3|007|type1|2010|0
name4|009|type2|2015|1
name5|002|type0|2016

What you are actually asking about? 您实际上在问什么?

You want to join the inputs based on field 1 in var1 and field 2 in var2. 您要基于var1中的字段1和var2中的字段2连接输入。 In that case I would suggest using join from coreutils which supports this directly, eg: 在那种情况下,我建议使用来自coreutils的join ,它直接支持此操作,例如:

join -t '|' -11 -22 -o'2.1 2.2 2.3 2.4 1.2' -a2 -eNA <(<<<"$var1") <(<<<"$var2")

Output: 输出:

name|location|type|built|Serial
name1|001|type1|2009|0
name2|003|type4|2012|1
name3|007|type1|2010|0
name4|009|type2|2015|1
name5|002|type0|2016|NA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM