shell编程读书笔记二

来源:互联网 发布:花生壳ddns 免费域名 编辑:程序博客网 时间:2024/04/30 14:11

  一、正则表达式的学习

  1,$s/[A-Z]/*/g         *不用反斜杠,因为出现在替换的字符串里头。
  同理

  such as *, ., [...], $, and ^

  也是这样。
 

^/(./)/1

first and second characters on the line if they're the same
二、cut
who | cut -c1-8,18-
tabs count as a single character when using the -c option.
cut -f1 phonebook   默认是tab为分隔符
sed -n l phonebook  用来把/t显示出来
三、paste
paste names addresses numbers 三个文件,记录放在一行
paste -d'+' names addresses numbers 记住单引号
paste -s names        Paste all lines from names 把一个文件的所有记录放在一行。
$ ls | paste -d' ' -s - Paste ls's output, use space as delimiter 
最后的破折号表示paste的来源是标准输入流,等价于
echo *
四、sed
默认是标准输入流,输出到标准输出流,所以并没有改变原始文件,输出每一行,无论有无改变,要改变原始文件,必须重定向
sed command filecommand 放在单引号之间
通过重定向修改文件:
$ sed 's/Unix/UNIX/' intro > temp Make the changes 只是改变每一行的第一个Unix
$ mv temp intro           And now make them permanent
全局改变
$ sed 's/Unix/UNIX/g' intro > temp   g代表是global,对每一行的所有的Unix 都进行改变
$ sed -n '/UNIX/p' intro  打印符合条件的行
$ sed '1,2d' intro      Delete lines 1 and 2