我需要一些帮助。
我想在bash中从以下两个文件行(仅从第二个文件行)进行解析,其中第二列是相同的,但利润第一列是唯一的:
file1
111403787651,111915870316631
111408649892,111917070744403
111408653841,111919750018614
111408655467,111917420005028
file2
111403787651,111915870316631
444444444441,111917070744403
222222222222,333333333333333
输出:仅从第二个文件
444444444441,111917070744403
感谢
awk
前往救援!
$ awk -F, 'NR==FNR{a[$2]=$1; next} $2 in a && $1 != a[$2]' file1 file2
444444444441,111917070744403
假设我已经正确阅读了您的意图(这是一个很大的假设,因为问题中的语言在很大程度上是不精确的),下面是一个本地bash实现,不需要外部工具,并在给定问题中的输入的情况下发出所需的输出:
#!/bin/bash
# ^^^^ - NOT /bin/sh, as this requires bash-only (indeed, bash-4.x+-only) features
# read first file's contents
declare -A first=( ) second=( ) # define associative arrays; requires bash 4.0
while IFS=, read -r a b; do # read columns into variables a and b
first[$a]=1; second[$b]=1 # set associative-array keys for each
done <file1 # ...doing the above reading from file1
# iterate through second file's contents
while IFS=, read -r a b; do # again, read into a and b
if [[ ${second[$b]} && ! ${first[$a]} ]]; then # if we already saw b, and did not see a
printf '%s,%sn' "$a" "$b" # ...then emit output.
fi
done <file2 # ...doing the above reading from file2
参考文献:
- BashFAQ#001("如何逐行(或逐字段)读取文件(数据流、变量)?")
- BashFAQ#006("如何使用[…]关联数组?")