发布网友 发布时间:2022-04-23 06:05
共2个回答
热心网友 时间:2023-09-10 06:37
#! /bin/bash
filea=a.txt
fileb=b.txt
n=0
m=0
while read linea
do
let "n=n+1"
noa1=`echo $linea|awk -F "|" '{print $1}'`
while read lineb
do
let "m=m+1"
nob1=`echo $lineb|awk -F "|" '{print $1}'`
nob2=`echo $lineb|awk -F "|" '{print $2}'`
if [ "$noa1" = "$nob1" ]
then
echo "${linea}|${nob2}">>c.txt
fi
done<$fileb
done<$filea
这个脚本已经可以把文件按照你要求合并了,不过按照时间字段比较我一时还没想出来怎么弄,请高手补充吧
热心网友 时间:2023-09-10 06:37
看看这个:
[seesea@UC ~]$ join -t'|' <(sort -t'|' -k1,1 -r -k5,5 a.txt | uniq -w3 | sort -t'|' -k1,1) <(sort -t'|' -k1,1 b.txt) > c.txt
[seesea@UC ~]$ cat c.txt
123|kkk|jjj|sss|2013-02-21 16:11:07|OFF
135|bbb|ccc|ddd|2013-01-28 16:11:07|ON
456|kkk|jjj|sss|2013-01-28 16:11:07|ON
7|kkk|jjj|sss|2013-02-21 16:11:07|OFF
-------------------
另一个方案:
[seesea@UC ~]$ awk -F'|' 'NR==FNR{b[$1]=$2}; NR!=FNR{a[$1]=$0 OFS b[$1]; ti[$1]=(ti[$1]<$5?$5:ti[$1])}; END{for (i in a){if (a[i]~ti[i]) print a[i]}}' b.txt a.txt > c.txt
[seesea@UC ~]$ cat c.txt
456|kkk|jjj|sss|2013-01-28 16:11:07 ON
123|kkk|jjj|sss|2013-02-21 16:11:07 OFF
135|bbb|ccc|ddd|2013-01-28 16:11:07 ON
7|kkk|jjj|sss|2013-02-21 16:11:07 OFF
-------------------
两方案都破坏原有的顺序,如果你要保持 a.txt 中的顺序,你另外做个排序吧