Swiss Army knife in the Linux Command Line"

Source: Internet
Author: User

The Swiss Army Knife mentioned here refers to the work that a simple command can complete a large piece of code in other advanced languages.

The following content is a summary of Joshua Levy on Quora:

Use sort/uniq to obtain the intersection, collection, and differences of the file content. Assume there are two text files, a and B, and the file itself has already removed the duplicate content. The following is the most efficient method, which can process files of any volume or even several GB. (Sort has no requirements for memory, but you may need to use the-T parameter .) You can try to compare it. You can see how many lines of code are required if Java is used to process the merge of files on the disk.

Copy codeThe Code is as follows: cat a B | sort | uniq> c # c is a collection of a and B
Cat a B | sort | uniq-d> c # c is the intersection of a and B
Cat a B | sort | uniq-u> c # c is different from B.

Summarize the sum of the third column of numbers in a text content (this method is three times faster than using Python and requires only 1/3 of the Code ):

Copy codeThe Code is as follows: awk '{x + = $3} END {print x}' myfile

If you want to view the file volume and modification date in a directory tree, use the following method, which is equivalent to "ls-l" in one directory ", in addition, the output format is more readable than "ls-lR:Copy codeThe Code is as follows: find.-type f-ls

Use the xargs command. This command is very powerful. Note that you can control how many items are executed on each line. If you are not sure that it is correctly executed, use xargs echo first. -I {} is also very useful. Example:

Copy codeThe Code is as follows: find.-name \ *. py | xargs grep some_function
Cat hosts | xargs-I {} ssh root @ {} hostname

Suppose you have a text file, such as a web server log, which has some values on some rows, such as the acct_id parameter in the URL. If you want to count all request records for each acct_id:

Copy codeThe Code is as follows: cat access. log | egrep-o 'acct _ id = [0-9] + '| cut-d =-f2 | sort | uniq-c | sort-rn]

[What are the most useful "Swiss army knife" one-liners on Unix?

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.