我有两个文本文件 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/