sed与unix命令等价代码鉴赏

发表于:2007-07-04来源:作者:点击数: 标签:
代码: UNIX | SED -------------+---------------------------------------------------------------- cat | sed ':' cat -s | sed '/./,/^/!d' tac | sed '1!G;h;!d' grep | sed '/patt/!d' grep -v | sed '/patt/d' head | sed '10q' head -1 | sed 'q' ta

  代码:
  UNIX     | SED
  -------------+----------------------------------------------------------------
  cat      | sed ':'
  cat -s    | sed '/./,/^$/!d'
  tac      | sed '1!G;h;$!d'
  grep     | sed '/patt/!d'
  grep -v   | sed '/patt/d'
  head     | sed '10q'
  head -1   | sed 'q'
  tail       | sed -e ':a' -e '$q;N;11,$D;ba'
  tail -1    | sed '$!d'
  tail -f     | sed -u '/./!d'
  cut -c 10  | sed 's/\(.\)\{10\}.*/\1/'
  cut -d: -f4 | sed 's/\(\([^:]*\):\)\{4\}.*/\2/'
  tr A-Z a-z  | sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'
  tr a-z A-Z  | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'
  tr -s ' '    | sed 's/ \+/ /g'
  tr -d '\012' | sed 'H;$!d;g;s/\n//g'
  wc -l      | sed -n '$='
  uniq      | sed 'N;/^\(.*\)\n\1$/!P;D'
  rev       | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
  basename | sed 's,.*/,,'
  dirname   | sed 's,[^/]*$,,'
  xargs     | sed -e ':a' -e '$!N;s/\n/ /;ta'
  paste -sd: | sed -e ':a' -e '$!N;s/\n/:/;ta'
  cat -n     | sed '=' | sed '$!N;s/\n/ /'
  grep -n    | sed -n '/patt/{=;p;}' | sed '$!N;s/\n/:/'
  cp orig new| sed 'w new' orig

原文转自:http://www.ltesting.net