sed&awk--2 sed基本操作
Like almost all UNIX programs, sed and awk can take input from standard input and send the output to standard output. If a filename is specified, input is taken from that file. The output contains the processed information. Standard output is the display screen, and typically the output from these programs is directed there. It can also be sent to a file, using I/O redirection in the shell, but it must not go to the same file that supplies input to the program.
sed和awk的共同选项 -f ,允许指定执行脚本的名字 sed -f scriptfile inputfile
sed和awk中,每个指令都包括2个部分: 模式和过程. 模式是由 / 分割的正则表达式.过程是一个或多个要执行的动作
sed自动输出行,awk不自动输出行.
sed基本操作只有在命令行上给出多个指令时才用-e选项
最好在任何情况下都要单引号包围指令,因为单引号阻止shell解释指令中特殊字符或者空格.
sed 's/ MA/, Massachusetts/' list 要替换的和被替换的都有空格,一定需要单引号括起
三种执行多重命令的方式:1. 用分号分割:
sed 's/ MA/, Massachusetts/;s/ PA/, Pennsylvania/' list
2. 指令前放 -e:
sed -e 's/ MA/, Massachusetts/' -e 's/ PA/, Pennsylvania/' list
3. Bourne Shell分号指令...(略) P31.
脚本文件sed -f scriptfile file
cat scriptfile
s/ MA/, Massachusetts/
s/ PA/, Pennsylvania/
保存输出>不能保存到当前编辑文件,会造成乱码
阻止输入行的自动显示
-n选项可以阻止输入行自动显示. -n需要在指令后加上打印命令p.
-n和p组合打印只受影响的行.
sed -n -e 's/MA/Massachusetts/p' list.txt
$ awk -F, '{ print $1; print $2; print $3 }' list
awk纵向排列
-F指定分隔符:
awk -F : '{print $1, $2,$3}'
指定:作为分隔符
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22383153/viewspace-671112/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/22383153/viewspace-671112/