如何比较 2 个文件并只打印出选定的信息?
How to compare 2 files and print out only selected info?
我正在尝试比较这 2 个文件,并且只打印出我需要的作为期望输出的内容。
File1:
012345:x:9012345:9012345:John Smith:/home/bin/bash
543210:x:9876543:9876543:Troy Denver:/home/bin/bash
111111:x:9898989:9898989:Mathew Moore:/home/bin/bash
222222:x:0101010:0101010:Chuck Maxwell:/homebin/bash
333333:x:1212121:1212121:Bob Evans:/home/bin/bash
File2:
333333 ALL=(ALL) NOPASSWD: ALL
543210 ALL=(ALL) NOPASSWD: ALL
222222 ALL=(ALL) NOPASSWD: ALL
Desired Output:
333333 Bob Evans
543210 Troy Denver
222222 Chuck Maxwell
这就是我所做的
readarray num1 < file1 | awk -F'[ ]' '{print }'
while read -r $num1;
do grep "$num1" file2
echo "$num1" | awk -F':' '{print ,}'
done < file3.txt
使用 GNU 连接、排序、sed 和 bash:
join -j 1 -t : <(sort File1) <(sed 's/ /:/' File2 | sort) -o 1.1,1.5 | sed 's/:/ /'
输出:
222222 Chuck Maxwell
333333 Bob Evans
543210 Troy Denver
参见:man join
我正在尝试比较这 2 个文件,并且只打印出我需要的作为期望输出的内容。
File1:
012345:x:9012345:9012345:John Smith:/home/bin/bash
543210:x:9876543:9876543:Troy Denver:/home/bin/bash
111111:x:9898989:9898989:Mathew Moore:/home/bin/bash
222222:x:0101010:0101010:Chuck Maxwell:/homebin/bash
333333:x:1212121:1212121:Bob Evans:/home/bin/bash
File2:
333333 ALL=(ALL) NOPASSWD: ALL
543210 ALL=(ALL) NOPASSWD: ALL
222222 ALL=(ALL) NOPASSWD: ALL
Desired Output:
333333 Bob Evans
543210 Troy Denver
222222 Chuck Maxwell
这就是我所做的
readarray num1 < file1 | awk -F'[ ]' '{print }'
while read -r $num1;
do grep "$num1" file2
echo "$num1" | awk -F':' '{print ,}'
done < file3.txt
使用 GNU 连接、排序、sed 和 bash:
join -j 1 -t : <(sort File1) <(sed 's/ /:/' File2 | sort) -o 1.1,1.5 | sed 's/:/ /'
输出:
222222 Chuck Maxwell 333333 Bob Evans 543210 Troy Denver
参见:man join