vma*_*hav 6 shell-script text-processing csv file-comparison columns
我有一个File1
包含数据的文件
aaa
bbb
Run Code Online (Sandbox Code Playgroud)
另一个文件File2
的数据为:
2,aaa,234
w,bbb,589
4,ccc,675
Run Code Online (Sandbox Code Playgroud)
我需要将File1
数据与 column2 数据进行比较,File2
并将匹配的数据打印到一个文件中,将不匹配的数据打印到另一个文件中。
awk -F '
!b{a[$0]; next}
$2 in a {print > "matching.txt"; next}
{print > "non-matching.txt"}' file1 b=1 file2
Run Code Online (Sandbox Code Playgroud)
或者通过两次传递并假设文件按连接键排序:
join -t , -2 2 -o 2.1,2.2,2.3 file1 file2 > matching.txt
join -t , -2 2 -v 2 -o 2.1,2.2,2.3 file1 file2 > non_matching.txt
Run Code Online (Sandbox Code Playgroud)