linux - AWK--比较两个不同文件中两个变量的值

标签 linux bash awk text-processing

我有两个文本文件 A.txt 和 B.txt。 A.txt的每一行 A.txt

100
222
398

B.txt

1  2  103  2 
4  5  1026 74
7  8  209  55
10 11 122  78

我正在寻找的是这样的:

for each line of A 
    search B;
    if (the value of third column in a line of B - the value of the variable in A > 10)
        print that line of B;

任何 awk 都可以做到这一点吗?

最佳答案

像这样的怎么样,

我在理解你的问题时遇到了一些困难,但这也许会给你一些指导,

#!/bin/bash 

# Read intresting values from file2 into an array, 
for line in $(cat 2.txt | awk '{print $3}') 
do 
  arr+=($line)
done 

# Linecounter, 
linenr=0 

# Loop through every line in file 1, 
for val in $(cat 1.txt) 
do 
  # Increment linecounter, 
  ((linenr++)) 

  # Loop through every element in the array (containing values from 3 colum from file2) 
  for el in "${!arr[@]}"; 
  do
    # If that value - the value from file 1 is bigger than 10, print values
    if [[ $((${arr[$el]} - $val )) -gt 10 ]] 
    then
      sed -n "$(($el+1))p" 2.txt
      # echo "Value ${arr[$el]} (on line $(($el+1)) from 2.txt) - $val (on line $linenr from 1.txt) equals  $((${arr[$el]} - $val )) and is hence bigger than 10" 
    fi 
   done
done

注意,

  • 这是一件快速而肮脏的事情,还有改进的空间。但我认为它可以完成任务。

关于linux - AWK--比较两个不同文件中两个变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19621114/

相关文章:

linux - 如何删除名称为 '' $'\r' 的文件?

linux - 如何修改行首有字符的模式

shell - 如何在文本的某一行末尾附加一些内容

linux - 加载使用 ptxdist 通过 i586_qemu 配置生成的镜像的虚拟机快照时发生内核 panic

linux - MPSTAT 仅在应用程序执行期间显示 cpu 使用率平均值

linux - Bash:使用文件名作为带通配符的命令

linux - 如何在考虑其他文件的情况下显示文件的内容?

c - 在 Linux 上显示视频

java - 0 NATIVE_LIBRARY_NAME 无法解析或不是 Linux + Eclipse + Java 的字段

regex - Bash Regex 接受 [Number],但我只想接受 Number