Shell practice: Check whether there are duplicates and extract Duplicated content

Source: Internet
Author: User
Check whether there are duplicates in the second column of the file, and several rows are duplicated, and extract the duplicate rows. The second column actually uses millions of rows, and there are more columns than this, A small part is listed here, as follows: cnyunwei. log092076261300442570717543306350921702423002508705309115 & check whether the second column in the nbs file is duplicated and several rows are duplicated, and extract the second column of the duplicate row
In actual work, there are millions of rows and more columns. Here we only list a small part, as shown below: cnyunwei. log
0920762613004 367707175433 0635
0921702423002 508705309115 1035
0922450613006 328209142523 5063
0927310523001 518207014922 1035
0920910523002 538510242124 6035
0920910613004 538311101228 1035
0921980613015 327806305726 1635
0922262313001 458706080015 1033
0920012313004 518306272410 9065
0926470213015 518207014922 5635
0925112313002 138512110026 1035
0928030613003 128601150031 1357
0927340123001 538311101228 1035
0926471413002 518803249270 8065
0926550523002 448706043468 1035
200908565489 148705080622 2083
200910633424 427703083341 1183
709101202008 138512110026 1035
0933001263450 337904124727 4258
709108057002 13801126122X1335
709200077043 338905250343 1035
0933001251479 138512110026 5258
709213077016 338912128329 1035
709213072032 338612152326 1335
709213072002 338705158523 1935
709214077003 339001015682 1035
==>> The following two methods can achieve the same effect
Awk-F "" '{print $2}' cnyunwei. log | sort-r | uniq-c | grep-v "1"
Awk-F "" '{print $2}' cnyunwei. log | sort-r | uniq-c | awk '{if ($1> 1) {print $0 }}'
Result After execution:
2 538311101228
2 518207014922
3 138512110026
Note:
Awk-F "" '{print $2}' cnyunwei. log | sort-r | uniq-c
==>> This means to extract the second column and filter duplicate rows and list repeated rows.
Expand to include the entire row of the above result ==>> Save the result temporarily in temp. log file, and then read the file to retrieve the entire line of content in the original file.

Awk-F "" '{print $2}' cnyunwei. log | sort-r | uniq-c | grep-v "1" | awk '{print $2}'> temp. log
Or
Awk-F "" '{print $2}' cnyunwei. log | sort-r | uniq-c | awk '{if ($1> 1) {print $0}' | awk '{print $2}'> temp. log
==>>
Vi cnyunwei. sh
#! /Bin/sh
SOCFILENAME = cnyunwei. log
FILENAME = temp. log

If [-e $ FILENAME]; then
Rm-rf $ FILENAME
Fi
Awk-F "" '{print $2}' $ SOCFILENAME | sort-r | uniq-c | grep-v "1" | awk '{print $2}'> $ FILENAME

While read LINE
Do
Grep $ LINE $ SOCFILENAME
Done <$ FILENAME
Exit 0
The simpler method is to merge it into a single line of command:
Awk-F "" '{print $2}' cnyunwei. log | sort-r | uniq-c | awk '{if ($1> 1) {print $0} '| awk' {print $2} '| while read output; do grep $ output cnyunwei. log; done
They are all practical shells at work. You can reply to the post to complete them!
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.