关于排序的一个脚本

    一家上海运维工程师的面试笔试题,关于脚本排序的。

 

 题目: 文件 1、 2、 ,只能使用 sort uniq uniq -d ,uniq -u命令做以下排序。

     X=1∩2∩3

     Y=1-2-3

     Z=1∪2∪3

 解析:

先假设文件1内容为 a b c e,文件2的内容为b c d,文件3的内容为c d e.

预期结果是:

X=1,Y=a,Z=a b c d e

解法如下,仅供参考和交流。当然不吝赐教,谢谢。

 脚本:

#!/bin/bash

#author yueyangbeihan

#只使用sort和uniq命令做以下排序

#x=1^2^3(取文件1 2 3的交集)

sort  /tmp/1 /tmp/2 | uniq -d  > /tmp/x.1

sort  /tmp/x.1 3 | uniq -d > /tmp/X

#y=1-2-3(取文件1中不包含在文件2和3中的)方法一

sort  /tmp/1 /tmp/2 /tmp/2 /tmp/3 /tmp/3 | uniq -u > /tmp/Y

#y=1-2-3=1-(2+3)方法2

#取2 3文件并集

sort  /tmp/2 /tmp/3 | uniq > /tmp/23

#y=1-23=1-(1^23)

sort /tmp/1 /tmp/23 | uniq -d > /tmp/y.1

sort /tmp/1 /tmp/y.1 | uniq -u > /tmp/y.bak

#此时y和y.bak是一摸一样的

#1U2U3取3个文件并集

sort 1 2 3 | uniq > /tmp/Z

你可能感兴趣的:(排序,职场,sort,uniq,休闲)