每日一题--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]}‘ file1 file2
awk ‘FNR==NR{h[$1]=$2;next}{print h[$2]}‘ file1 file2
//next 满足前面这个条件就不执行后面的内容了

#!/bin/bash
for i in `awk ‘{print $1}‘ file1`
do
    for j in `awk ‘{print $2}‘ file2`
        do
        if [ $i == $j ];then
            awk ‘$1~/‘$i‘/{print $2}‘ file1
        fi
        done
done

原文地址:http://blog.51cto.com/13447608/2298481

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

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

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

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 "该脚本只用来对比一个文件比另一个文件多

用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

将字符串添加到指定的文件中去 AppendAllText ;判断指定路径的文件是否存在File.Exists(Path)

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { static void Main(string[] args) { string path = @"F:\1.txt"; //指定文件的路径 //---------------------------------------------- //.Exi

替换文件中某个字符串并写入新内容(Java代码实现)

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStream; import java.io.InputStreamReader; /* * 替换文件(如果该文件含有子目录,则包括子目录所有文件)中某个字符串并写入新内容(J

在布局文件/layout中加的id,在R文件中却没有显示?

新建android工程,在/layout/activity_main.xml文件中,新建了一个Button, <Button android:id="@+id/person" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/person" /> 但是,该ID资源person却

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

思路: 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

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 >

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