原文:https://www.quora.com/What-are-the-most-useful-Swiss-army-knife-one-liners-on-Unix
A few simple but useful examples:
- Set intersection, union, and difference of text files via sort/uniq: Suppose a and b are text files that are already uniqued. Note this is fast, and works on files of arbitrary size, up to many gigabytes. (Sort is not limited by memory, though you may need to use -T.) For comparison, see how many lines it is to write a disk-based merge sort in Java.
cat a b | sort | uniq > c # c is a union b
cat a b | sort | uniq -d > c # c is a intersect b
cat a b b | sort | uniq -u > c # c is set difference a - b
- Summing all numbers in the third column of a text file (this is probably 3X faster and 3X less code than equivalent Python):
awk '{ x += $3 } END { print x }' myfile
- If want to see sizes/dates on a tree of files, this is like a recursive "ls -l" but is easier to read than "ls -lR":
find . -type f -ls
- Use xargs. It's very powerful. Note you can control how many items execute per line (-L) as well as parallelism (-P). If you're not sure if it'll do the right thing, use xargs echo first. Also, -I{} is handy. Examples:
find . -name \*.py | xargs grep some_function
cat hosts | xargs -I{} ssh root@{} hostname
- Say you have a text file, like a web server log, and a certain value that appears on some lines, such as an acct_id parameter that is present in the URL. If you want a tally of how many requests for each acct_id:
cat access.log | egrep -o 'acct_id=[0-9]+' | cut -d= -f2 | sort | uniq -c | sort -rn