Efficient Linux at the Command
Line中给了我们wc, head, cut,
grep, sort,
uniq六个命令,我们用这六个命令学习管道技巧。
下面是示例文件animals.txt:
1 2 3 4 5 6 7
python Programming Python 2010 Lutz, Mark snail SSH, The Secure Shell 2005 Barrett, Daniel alpaca Intermediate Perl 2012 Schwartz, Randal robin MySQL High Availability 2014 Bell, Charles horse Linux in a Nutshell 2005 Sielver, Ellen donkey Cisco IOS in a Nutshell 2005 Bodney, James oryx Writting Word Macros 1999 Roman, Steven
horse Linux in a Nutshell 2005 Sielver, Ellen donkey Cisco IOS in a Nutshell 2005 Bodney, James
使用-v命令可以查看animals.txt中不含有字符串Nutshell的行:
1
grep -v Nutshell animals.txt
1 2 3 4 5
python Programming Python 2010 Lutz, Mark snail SSH, The Secure Shell 2005 Barrett, Daniel alpaca Intermediate Perl 2012 Schwartz, Randal robin MySQL High Availability 2014 Bell, Charles oryx Writting Word Macros 1999 Roman, Steven
总之,grep命令在查找文本中十分有用。下面的命令展示了含有Perl的*.txt文件:
1
gerp Perl *.txt
1 2 3
animals.txt:alpaca Intermediate Perl 2012 Schwartz, Randal p.txt:Perl programming language. p.txt:languages such as Perl, Python, PHP and Ruby
alpaca Intermediate Perl 2012 Schwartz, Randal donkey Cisco IOS in a Nutshell 2005 Bodney, James horse Linux in a Nutshell 2005 Sielver, Ellen oryx Writting Word Macros 1999 Roman, Steven python Programming Python 2010 Lutz, Mark robin MySQL High Availability 2014 Bell, Charles snail SSH, The Secure Shell 2005 Barrett, Daniel
或以降序排列(使用-r选项):
1
sort -r animals.txt
1 2 3 4 5 6 7
snail SSH, The Secure Shell 2005 Barrett, Daniel robin MySQL High Availability 2014 Bell, Charles python Programming Python 2010 Lutz, Mark oryx Writting Word Macros 1999 Roman, Steven horse Linux in a Nutshell 2005 Sielver, Ellen donkey Cisco IOS in a Nutshell 2005 Bodney, James alpaca Intermediate Perl 2012 Schwartz, Randal