The "Swiss Army Knife" here refers to the work of a simple command that can complete a large chunk of code in other high-level languages.
The following content is a summary of the Joshua Levy on Quora's website:
Get the intersection, collection, and difference of the contents of the file through Sort/uniq: Suppose there are two text files in a and B, the file itself has been removed from the duplicate content. Here is the most efficient way to handle any volume of files, even several G files. (Sort does not require memory, but you may need to use the-t argument.) You can try to compare, and you can see how many lines of code would be needed if you were to use Java to handle merging files on disk.
Copy Code code as follows:
Cat A B | Sort | Uniq > C # C is the collection of A and B
Cat A B | Sort | uniq-d > C # C is the intersection of a and b
Cat A b b | Sort | Uniq-u > C # c is different from A and b
sum up the sum of the third column of a text content (this method is 3 times times faster than in Python and requires only 1/3 of the amount of code):
Copy Code code as follows:
awk ' {x + = $} end {print x} ' myfile
If you want to see the volume and date of a file in a directory tree, use the following method, which is equivalent to "Ls-l" in your directory, and the output is more readable than you would use "LS-LR":
Copy Code code as follows:
Use the Xargs command. This command is very powerful. Notice how many things you can control on each line. If you're not sure it's the correct execution, use Xargs echo first. Similarly,-i{} is also very useful. Example:
Copy Code code 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, with some values on some lines, such as the acct_id parameter in the URL. If you want to count all request records for each acct_id:
Copy Code code as follows:
Cat Access.log | Egrep-o ' acct_id=[0-9]+ ' | cut-d=-f2 | Sort | uniq-c | SORT-RN]
[Original: What are the most useful "Swiss army knife" one-liners on Unix?