A legend is the shell test of Baidu interview

Source: Internet
Author: User
Tags diff

"Problem" script implementation, you can use Shell, Perl and so on. Save all the rows in file B, but none in file a, to file C and count the number of lines in C.

To translate adult words is, suppose that there is a file a is:

A

B

C

D


File B is:

1

2

3

4

A

B


Now ask for the output "B has a no" line, that is 1 2 3 4, and then wc-l a bit.


"ideas" two files comparison, the first idea is a diff, but the diff either C or Y will involve a lot of > < +-do not say, and the diff command is a straightforward contrast, even if the letter is the same row, it will be a diff record. If you also want to use the for in statement will not be clear to solve the problem, so you have to change a method.


The second method is the Comm command, but this command has a premise, that is, to sort, comm is better than diff is that it only compares the content and does not mean the same line, but pay attention to the comparison of the file. #comm -12 A b is to find "A and B have" items,#comm -23 a b is to find "B" and "a".


"solution"Perl I will not, I will write with the shell:

#!/bin/bash#written by Chrischan @ 2016-4-21sort A.txt>a1.txt #排序, otherwise there will be a hint sort b.txt>b1.txtcomm-23 b1.txt a1.t XT >c.txt #由于是要找b有a没有的, b written in front, a written in the rear echo $ (cat c.txt|wc-l)

In fact, there is a more simple, only in one sentence:

Grep-v-X b.txt-f a.txt|wc-l


Many books do not write grep-x-f meaning, here to fill:

-F: Specifies the template file whose contents contain one or more template styles so that grep finds the contents of the file that match the template criteria, in the form of a template style for each column.

-X: Displays only columns that are compliant with all columns.


From a problem can easily see the ability level of the shell, with the diff dead entanglement is the primary, with Comm is intermediate, and grep is advanced. It's a good question indeed.

This article is from "Life is waiting for Gordo" blog, please make sure to keep this source http://chenx1242.blog.51cto.com/10430133/1766450

A legend is the shell test of Baidu interview

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.