sed&awk--2 sed基本操作

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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值