Chinaunix

标题: 输出每行的头四个字符 [打印本页]

作者: luofeiyu_cu    时间: 2012-12-18 21:01
标题: 输出每行的头四个字符
sed p -n '/^../'  test  
为何不行?

作者: Shell_HAT    时间: 2012-12-18 21:04
  1. sed -r 's/(^....).*/\1/' urfile
复制代码
  1. grep -o '^....' urfile
复制代码

作者: Shell_HAT    时间: 2012-12-18 21:12
  1. awk 'BEGIN{FS=OFS=""}NF=4' urfile
复制代码
  1. awk '{print gensub(/^(....).*/,"\\1",1)}' urfile
复制代码

作者: cuteorange    时间: 2012-12-19 09:18
本帖最后由 cuteorange 于 2012-12-19 09:18 编辑

回复 1# luofeiyu_cu


    cut -c 1-4 test
作者: kernel69    时间: 2012-12-19 10:30
perl -lane 'print substr($_,0,4)' file
回复 1# luofeiyu_cu


   
作者: dahaoshanhe    时间: 2012-12-19 10:46
cut最适合做这事了
作者: aimar111    时间: 2012-12-24 21:04
新手也来学习下

sed -n 's/\(^.\{4\}\).*/\1/p' urfile
作者: 07101060407    时间: 2012-12-25 13:42
cat test|while read a;do echo ${a:0:4};done
作者: Shell_HAT    时间: 2012-12-26 09:04
回复 7# aimar111


-n和p是多余的
  1. sed 's/\(^.\{4\}\).*/\1/' urfile
复制代码

作者: aimar111    时间: 2012-12-26 10:08
回复 9# Shell_HAT


    刚试了一下,确实不需要参数,细节还得注意啊呵呵,跟高手学习了




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2