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?