【问题标题】:how to compare two text file with first column if match then print same if not then put zero?如果匹配,如何将两个文本文件与第一列进行比较,如果不匹配则打印相同然后输入零?
【发布时间】:2018-07-17 04:34:56
【问题描述】:

1.txt 包含

1  
2  
3  
4  
5  
.  
.  
180    

2.txt 包含

3  0.5  
4  0.8  
9  9.0  
120  3.0  
179  2.0  

所以我想要我的输出,比如如果 2.txt 与 1.txt 的第一列匹配,那么应该打印 2.txt 中第二列的值。而如果不匹配,则应打印零。

类似的输出应该是:

1  0.0  
2  0.0  
3  0.5  
4  0.8  
5  0.0  
.  
.  
8 0.0  
9 9.0
10 0.0  
11 0.0  
.  
.  
.  
120 3.0  
121 0.0  
.  
.  
150 0.0  
.  
179 2.0  
180 0.0  

【问题讨论】:

  • 谢谢..你能帮我回答这个问题吗?
  • 到目前为止你尝试了什么?
  • 使用 bash、join、sort、awk:join <(sort 1.txt) <(sort 2.txt) -a 1 | sort -n | awk '$2==""{$2="0.0"; print}'
  • 请避免"Give me the codez" 问题。而是显示您正在处理的脚本并说明问题所在。另见How much research effort is expected of Stack Overflow users?

标签: linux shell join awk sed


【解决方案1】:
awk 'NR==FNR{a[$1]=$2;next}{if($1 in a){print $1,a[$1]}else{print $1,"0.0"}}' 2.txt 1.txt

简要说明,

  1. NR==FNR{a[$1]=$2;next:将2.txt的$1记录到数组a中
  2. 如果数组a中存在1.txt中的$1,则打印a[$1],否则打印0.0

【讨论】:

    【解决方案2】:

    您能否尝试关注一下,如果这对您有帮助,请告诉我。

    awk 'FNR==NR{a[$1];next} {for(i=prev+1;i<=($1-1);i++){print i,"0.0"}}{prev=$1;$1=$1;print}'  OFS="\t" 1.txt 2.txt
    

    代码说明:

    awk '
    FNR==NR{                       ##Checking condition FNR==NR which will be TRUE when 1.txt is being read.
      a[$1];                       ##Creating an array a whose index is $1.
      next                         ##next will skip all further statements from here.
    }
    {
      for(i=prev+1;i<=($1-1);i++){ ##Starting a for loop from variable prev+1 to till value of first field with less than 1 to it.
        print i,"0.0"}             ##Printing value of variable i and 0.0 here.
    }
    {
      prev=$1;                     ##Setting $1 value to variable prev here.
      $1=$1;                       ##Resetting $1 here to make TAB output delimited in output.
      print                        ##Printing the current line here.
    }'  OFS="\t" 1.txt 2.txt       ##Setting OFS as TAB and mentioning Input_file(s) name here.
    

    上述代码的执行:

    输入文件:

    cat 1.txt
    1
    2
    3
    4
    5
    6
    7
    cat 2.txt
    3  0.5
    4  0.8
    9  9.0
    

    输出如下:

    awk 'FNR==NR{a[$1];next} {for(i=prev+1;i<=($1-1);i++){print i,"0.0"}}{prev=$1;$1=$1;print}'  OFS="\t" 1.txt 2.txt
    1       0.0
    2       0.0
    3       0.5
    4       0.8
    5       0.0
    6       0.0
    7       0.0
    8       0.0
    9       9.0
    

    【讨论】:

    • 投反对票,请在这里告诉我投反对票的原因吗?
    • 不是我的反对意见,但我们有很多非常相似的问题。查找副本可能是消磨时间的更好方式。
    【解决方案3】:

    这可能对你有用(GNU sed):

    sed -r 's#^(\S+)\s.*#/^\1\\s*$/c&#' file2 | sed -i -f - -e 's/$/ 0.0/' file1
    

    从 file2 创建一个 sed 脚本,如果 file2 的第一个字段与 file1 的第一个字段匹配,则将匹配行更改为 file2 中匹配行的内容。然后将所有其他行归零,即未更改的行附加了0.0

    【讨论】:

      猜你喜欢
      • 2021-06-26
      • 2014-08-28
      • 2021-10-30
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      相关资源
      最近更新 更多