使用sed和cut的一些小例子

关于SED:

修改第23行的内容:sed -i '23s/Applications/reb_app/g' reb_test.txt

删除最初三行:sed -i '1,3d' reb_test.txt

显示第二行:sed -n '2p' reb_test.txt

显示一个文件中1/3/5行中每行第15个字符:sed -n "1p;3p;5p" reb_test.txt | cut -c 1-5

打印出文件的前三行:head -n3 reb_test.txt  or sed -n "1,3p" reb_test.txt or sed "3q" reb_test.txt

打印出文件里面的全部内容:awk '{print}' ./reb_test.txt

/分割的第一部分($1)和第三部分($3)awk -F "/" '{print $1 $3}' ./reb_test.txt

显示文件的1/3awk 'NR==1 || NR==3{print $0}' reb_test.txt

关于CUT:

rebcheng@beleod22 shell $ who | grep rebcheng | head -n 3

rebcheng pts/8        2014-06-30 09:24 (10.220.115.102:6.0)

rebcheng pts/11       2014-08-15 09:33 (10.220.115.102:6.0)

rebcheng pts/58       2014-06-30 12:29 (10.220.115.102:6.0)

rebcheng@beleod22 shell $ who | grep rebcheng | head -n 3 | cut -b 6

e

e

e

rebcheng@beleod22 shell $ who | grep rebcheng | head -n 3 | cut -b -6

rebche

rebche

rebche

rebcheng@beleod22 shell $ who | grep rebcheng | head -n 3 | cut -d . -f 1

rebcheng pts/8        2014-06-30 09:24 (10

rebcheng pts/11       2014-08-15 09:33 (10

rebcheng pts/58       2014-06-30 12:29 (10

rebcheng@beleod22 shell $ who | grep rebcheng | head -n 3 | cut -d . -f 2

220

220

220

 

下面是转载的,没有自己试过:

[rocrocket@rocrocket programming]$ cat cut_ch.txt

星期一

星期二

星期三

星期四

[rocrocket@rocrocket programming]$ cut -b 3 cut_ch.txt

[rocrocket@rocrocket programming]$ cut -c 3 cut_ch.txt

看到了吧,用-c则会以字符为单位,输出正常;而-b只会傻傻的以字节(8位二进制位)来计算,输出就是乱码。

cut有哪些缺陷和不足:cut只擅长处理“以一个字符间隔”的文本内容。在处理多空格时有问题。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值