codecamp

一行代码

一些命令组合的例子:

  • 当你需要对文本文件做集合交、并、差运算时,结合使用 sort/uniq 很有帮助。假设 a 与 b 是两内容不同的文件。这种方式效率很高,并且在小文件和上G的文件上都能运用 (sort 不被内存大小约束,尽管在 /tmp 在一个小的根分区上时你可能需要 -T 参数),参阅前文中关于 LC_ALL 和 sort 的 -u 参数的部分。
      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
  • 使用 grep . * 来阅读检查目录下所有文件的内容,例如检查一个充满配置文件的目录比如/sys/proc/etc

  • 计算文本文件第三列中所有数的和(可能比同等作用的 Python 代码快三倍且代码量少三倍):
      awk '{ x += $3 } END { print x }' myfile
  • 如果你想在文件树上查看大小\日期,这可能看起来像递归版的 ls -l 但比 ls -lR 更易于理解:
      find . -type f -ls
  • 假设你有一个类似于 web 服务器日志文件的文本文件,并且一个确定的值只会出现在某些行上,假设一个 acct_id参数在URI中。如果你想计算出每个 acct_id 值有多少次请求,使用如下代码:
      cat access.log | egrep -o 'acct_id=[0-9]+' | cut -d= -f2 | sort | uniq -c | sort -rn
  • 运行这个函数从这篇文档中随机获取一条小技巧(解析 Markdown 文件并抽取项目):
      function taocl() {
        curl -s https://raw.githubusercontent.com/jlevy/the-art-of-command-line/master/README.md |
          pandoc -f markdown -t html |
          xmlstarlet fo --html --dropdtd |
          xmlstarlet sel -t -v "(html/body/ul/li[count(p)>0])[$RANDOM mod last()+1]" |
          xmlstarlet unesc | fmt -80
      }
系统调试
冷门但有用
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }