Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux AWK——比较两个不同文件中两个变量的值_Linux_Bash_Awk_Text Processing - Fatal编程技术网

Linux AWK——比较两个不同文件中两个变量的值

Linux AWK——比较两个不同文件中两个变量的值,linux,bash,awk,text-processing,Linux,Bash,Awk,Text Processing,我有两个文本文件A.txt和B.txt。A.txt的每一行 A.txt B.txt 我要找的是这样的东西: 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; cat f1 1 4 9 16 cat f2 2 4 10 8 3 9 20

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

B.txt

我要找的是这样的东西:

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;
cat f1
1
4
9
16

cat f2
2 4 10 8
3 9 20 8
5 1 15 8
7 0 30 8

awk 'FNR==NR{a[NR]=$1;next} $3-a[FNR] < 10' f1 f2
2 4 10 8
5 1 15 8
任何这样做的awk???

使用如下awk:

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;
cat f1
1
4
9
16

cat f2
2 4 10 8
3 9 20 8
5 1 15 8
7 0 30 8

awk 'FNR==NR{a[NR]=$1;next} $3-a[FNR] < 10' f1 f2
2 4 10 8
5 1 15 8

看看与嵌套for循环相比,基于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
注意,

  • 这是一件又快又脏的事情,还有改进的余地。但我认为它会成功的

这在几乎任何脚本语言中都是一个微不足道的练习。你已经试过什么了?我是shell脚本的初学者,不知道如何处理两个不同的文件…如果你想用一些shell脚本语言完成这项工作,你应该指定哪个并添加一个适当的标记。我个人推荐Python来做这类事情。谢谢你的回答和建议。对不起,但我注意到你的代码没有查找f2中的所有行,只是将f1中每个变量的值与同一行中f2的第三个变量的值进行比较。但我想做的是,对于f1中的每个值,解析所有f2行,如果条件为真,则打印该行。这是因为您最初的问题没有明确说明这一点,我必须假设样本数据和预期的响应。看看你编辑的问题,这仍然可以通过更简单的方式使用awk来完成,而不是像在接受的答案中那样使用嵌套for循环。谢谢你,但是,我希望打印整个2.txt行,而不是执行回显!!你能告诉我怎么做吗?好的,注释掉回音行,改为打印2.txt中的行。
#!/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