The role of *grep-f/f *

Source: Internet
Author: User
Tags sort

(1.) grep-f yourstring-r Path

Function: Search for strings in documents with grep

[Root@sor_sys hahah]# grep-f 0576-r/root/zy/hahah
/root/zy/hahah/b:05766798607
/root/zy/hahah/b:05766798608
/root/zy/hahah/b:05766798609
/root/zy/hahah/a:05766798608

(2.) grep-v-F file1 file2

Function: Outputs the contents of file 2, but rejects the content contained in file 1

Let's look at an application:

[Root@sor_sys hahah]# Cat A
1
4
05766798608
05766798608
6
7
[Root@sor_sys hahah]# Cat B
05766798607
05766798608
05766798609
[Root@sor_sys hahah]# grep-v-F B a |tee 222 | Wc-l
4
[Root@sor_sys hahah]# Cat 222
1
4
6
7

PS:

Tee Syntax: Tee [-a] [-i] [File ...]
Function: equivalent to echo plus >, the standard input output to the standard input and write to the file

-A: (add) does not overwrite the original content, added to the back of the file

-I: (ignore) is not interrupted if not completed

Lists the contents of the text file Slayers.story, and copies 3 copies, with the file names Ss-copy1, Ss-copy2, Ss-copy3, respectively:

Cat slayers.story |tee ss-copy1 ss-copy2 ss-copy3

(3.) grep-f-F file1 file2

function: You can output the line of file 1 in file 2

But I did an experiment, and if there are no duplicate rows in file 1, the result is correct. If there are duplicate rows, the result is the same as a row with no duplicates.

I want to achieve the goal is that the file 1 put the mobile phone number of the first 7, file 2 put the mobile phone number of the first 7 and the provinces represented, output and then statistics file 1 each province has the number of mobile phone numbers.

[Root@sor_sys hahah]# Cat File1
11111
22222
11111
22222
33333
44444
55555
[Root@sor_sys hahah]# Cat File2
11111 BJ
22222 HB
33333 HN
44444 NM
55555 XM
66666 MK

[Root@sor_sys hahah]# grep-f-F file1 file2
11111 BJ
22222 HB
33333 HN
44444 NM
55555 XM
[Root@sor_sys hahah]#

It seems that this is not possible, there are 2 in the File1 11111 and 2 22222, that is, if the file1 has duplicate rows, also repeat the output.

[Root@sor_sys hahah]# join-1 2-2 1 < (sort file1|uniq-c) < (sort file2)
11111 2 BJ
22222 2 HB
33333 1 HN
44444 1 NM
55555 1 XM


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.