其中有一些是我写的,其它都是汇总shell群里面的大牛写的,大家有更好的方法也可以提出来。

要求:把第一个数字和后面的“/”去掉

  1. [root@yunwei14 scripts]# cat StringCut.txt  
  2. 123/ 
  3. 456/ 
  4. 789/ 

脚本实例:

  1. [root@yunwei14 scripts]# sed 's/^.//g;s/\///g' StringCut.txt                                         
  2. 23 
  3. 56 
  4. 89 
  5. [root@yunwei14 scripts]# cat StringCut.txt |cut -c 2-3 
  6. 23 
  7. 56 
  8. 89 
  9. [root@yunwei14 scripts]# cat StringCut.txt |while read String;do echo ${String:1:2};done 
  10. 23 
  11. 56 
  12. 89 
  13. [root@yunwei14 scripts]# grep -oP '(?<=.).*(?=/)' StringCut.txt  
  14. 23 
  15. 56 
  16. 89 
  17. [root@yunwei14 scripts]# for i in $(cat StringCut.txt);do expr substr "$i" 2 2; done                 
  18. 23 
  19. 56 
  20. 89 
  21. [root@yunwei14 scripts]# awk 'sub("^.","")sub("/",""){print $0}' StringCut.txt                       
  22. 23 
  23. 56 
  24. 89 
  25. [root@yunwei14 scripts]# awk 'BEGIN{FS=OFS=""}NF=3,sub(/^./,"")' StringCut.txt  
  26. 23 
  27. 56 
  28. 89 
  29. [root@yunwei14 scripts]# awk '{print substr($0,2,2)}' StringCut.txt                                  
  30. 23 
  31. 56 
  32. 89 
  33. [root@yunwei14 scripts]# awk -vFS='/' '$1=substr($1,2)' StringCut.txt  
  34. 23  
  35. 56  
  36. 89  
  37. [root@yunwei14 scripts]# awk -F '/' '{print substr ($1,2,3)}' StringCut.txt  
  38. 23 
  39. 56 
  40. 89