awk实现两个文件中数据集求同异

1.在B中同时也在A中,comm_a_b.sh

sort -nu A > /tmp/A.txt

sort -nu B > /tmp/B.txt

awk ‘NR==FNR{a[$1]=$2} NR>FNR {if($1 in a){print $0}}‘ /tmp/A.txt /tmp/B.txt > /tmp/result.txt

2.在B中,不在A中,diff_in_b_not_in_a.sh

sort -nu A > /tmp/A.txt

sort -nu B > /tmp/B.txt

awk ‘NR==FNR{a[$1]=$2} NR>FNR {if(!($1 in a)){print $0}}‘ /tmp/A.txt /tmp/B.txt > /tmp/result.txt

时间: 2024-08-05 14:13:28

awk实现两个文件中数据集求同异的相关文章

比较两个文件中,一个文件比另一个文件多的行

1. 该脚本用来比较两个文件中,其中一个文件比另一个文件多的行,常用来工作环境中,对比得出多余的ip地址 #!/bin/bash #different in file1 and file2 #author:vaedit #date:2017/8/20 #read -p "请输入第一个文件路径" file1 #read -p "请输入第二个文件路径" file2 function print_help(){ echo "该脚本只用来对比一个文件比另一个文件多

awk合并两个文件

awk 合并两个文件: awk 'NR==FNR{a[i]=$0;i++}NR>FNR{print a[j]"    "$0;j++}' template interface > interface_last template: Template Log tqt_url-response-code Template Log tqt_url-response-code Template Log tqt_url-response-code Template Log tqt_ur

awk 对两个文件进行合并操作

1.awk命令概念 $0 表示一个文本中的一行记录 $1...N 表示一行中的第 1...N 字段 FNR     The input record number in the current input file.  #已读入当前文件的记录数 NR      The total number of input records seen so far.      #已读入的总记录数 next    Stop processing the current input record. The nex

使用 awk 过滤文本或文件中的字符串

当我们在 Unix/Linux 下使用特定的命令从字符串或文件中读取或编辑文本时,我们经常需要过滤输出以得到感兴趣的部分.这时正则表达式就派上用场了. 什么是正则表达式? 正则表达式可以定义为代表若干个字符序列的字符串.它最重要的功能之一就是它允许你过滤一条命令或一个文件的输出.编辑文本或配置文件的一部分等等. 正则表达式的特点 正则表达式由以下内容组合而成: 普通字符,例如空格.下划线.A-Z.a-z.0-9. 可以扩展为普通字符的元字符,它们包括: (.) 它匹配除了换行符外的任何单个字符.

每日一题--4--在两个文件中取交集,显示指定的内容

把这个两个文件都存在的用户的密码输出出来 [[email protected] student]# head file1 file2 ==> file1 <== oldboy 1234 alex 4567 lidao 9999 ==> file2 <== 001 lidao 002 alex 003 oldboy 004 oldgirl 提示:需要用到如何判断这两个文件不是一个文件. 解题思路 awk 'FNR==NR{h[$1]=$2}FNR!=NR{print h[$2]}'

用python比较两个文件中内容的不同之处, 并输出行号和内容.

代码部分: '''cmpfile.py - 比对两个文件, 如果有不同之处, 打印内容和行号''' import os class cmpFile: def __init__(self, file1, file2): self.file1 = file1 self.file2 = file2 def fileExists(self): if os.path.exists(self.file1) and os.path.exists(self.file2): return True else: r

合并两个文件中,相同的内容

思路: 1.先写一个函数function(line,file):判断line是否在file中. 2.将一个文件的行循环运行上面的函数. def find_line(line,file): f=file.readline() while True: if not f: break if f==line: return line else: return f=file.readline() f_a=open("a.txt") f_b=open("b.txt") f_c=

paste:linux合并两个文件中的列(左右合并)

[[email protected] ~]# paste [-d] file1 file2 选项与参数: -d  :后面可以接分隔字符.默认是以 [tab] 来分隔的! -   :如果 file 部分写成 - ,表示来自 standard input 的数据的意思. 范例一:将 /etc/passwd 与 /etc/shadow 同一行贴在一起 [[email protected] ~]# paste /etc/passwd /etc/shadow bin:x:1:1:bin:/bin:/sbi

Python-查找两个文件中相同的ip地址

with open("testt","r") as f1: list1 = f1.readlines() print(list1) list1 = set(list1) with open("test2","r") as f2: list2 = f2.readlines() print(list2) list2 = set(list2) same_data = list1.intersection(list2) print(s