Cut 命令

cut命令是用来切割数据的, 没错, 竖着割.

刚才搞到三蛋的库子, 那么我就拿这个数据库做例子吧 :)

首先看文件里有什么:

  1. cat pass
  2. ahmed:sales@samaaegy.net:197.44.60.223:Ahmed2542604
  3. ahmed:sales@spctec.com:41.196.193.71:AAHMEDEMAD944405
  4. Ahmed:sales@time2timegroup.com:2.49.209.135:a13c14zI1Nwdck3a
  5. ahmed:sales1@safetymisr.com.eg:197.44.60.223:ahmed2542604
  6. ahmed:salesway@msn.com:216.241.47.198:hggih;fv
  7. Ahmed:salfiste.hacker@gmail.com:41.225.249.155:suffeitula.10@
  8. ahmed:salhexp@gmail.com:217.55.82.202:magicmasterz01
  9. ahmed:salihomer681@yahoo.com:2.89.211.62:123qwe
  10. Ahmed:salikok@live.com:87.109.81.252:faraz54321
  11. ahmed:salma.ali1993@yahoo.com:217.26.255.72:asdf1234
  12. ahmed:salma_love8226@yahoo.com:41.238.81.106:medo0125593126
  13. ahmed:salmad123@gmail.com:81.192.238.123:sonunigam1
  14. ahmed:salmamalikmother@gmail.com:65.49.14.11:love-you2
  15. ahmed:salman.a7med@gmail.com:41.137.61.65:azerty123654789
  16. ahmed:salmankhan200007@gmail.com:65.49.14.71:123asdzxc
  17. ahmed:salmanlakho813@gmail.com:182.182.14.9:2k11swe75

好, 里面是以 用户名:邮箱:IP地址:密码 这种格式存储的, 那么,如果我们要想提取处里面所有的密码该怎么办呢?

这就是cut的威力了:

  1. cut -d : -f 4 pass
  2. Ahmed2542604
  3. AAHMEDEMAD944405
  4. a13c14zI1Nwdck3a
  5. ahmed2542604
  6. hggih;fv
  7. suffeitula.10@
  8. magicmasterz01
  9. 123qwe
  10. faraz54321
  11. asdf1234
  12. medo0125593126
  13. sonunigam1
  14. love-you2
  15. azerty123654789
  16. 123asdzxc
  17. 2k11swe75

详细解释一下这条命令, 其中-d参数后面跟的是分隔符, 我们看到, 里面的数据都是用:分隔的, 所以用-d :把每一列分开, 然后的-f代表第几列, f也就是filed的意思. 由于密码是在第4个字段, 所以这里是-f 4.

提取用户名+密码呢?

  1. cut -d : -f 1,4 pass #用','分隔我们要选取的字段
  2. ahmed:Ahmed2542604
  3. ahmed:AAHMEDEMAD944405
  4. Ahmed:a13c14zI1Nwdck3a
  5. ahmed:ahmed2542604
  6. ahmed:hggih;fv
  7. Ahmed:suffeitula.10@
  8. ahmed:magicmasterz01
  9. ahmed:123qwe
  10. Ahmed:faraz54321
  11. ahmed:asdf1234
  12. ahmed:medo0125593126
  13. ahmed:sonunigam1
  14. ahmed:love-you2
  15. ahmed:azerty123654789
  16. ahmed:123asdzxc
  17. ahmed:2k11swe75

提取邮箱后面的呢?

  1. cut -d : -f 2- pass #用'-'表示从哪儿到哪儿,如果不填则表示最后(或者最前)
  2. sales@samaaegy.net:197.44.60.223:Ahmed2542604
  3. sales@spctec.com:41.196.193.71:AAHMEDEMAD944405
  4. sales@time2timegroup.com:2.49.209.135:a13c14zI1Nwdck3a
  5. sales1@safetymisr.com.eg:197.44.60.223:ahmed2542604
  6. salesway@msn.com:216.241.47.198:hggih;fv
  7. salfiste.hacker@gmail.com:41.225.249.155:suffeitula.10@
  8. salhexp@gmail.com:217.55.82.202:magicmasterz01
  9. salihomer681@yahoo.com:2.89.211.62:123qwe
  10. salikok@live.com:87.109.81.252:faraz54321
  11. salma.ali1993@yahoo.com:217.26.255.72:asdf1234
  12. salma_love8226@yahoo.com:41.238.81.106:medo0125593126
  13. salmad123@gmail.com:81.192.238.123:sonunigam1
  14. salmamalikmother@gmail.com:65.49.14.11:love-you2
  15. salman.a7med@gmail.com:41.137.61.65:azerty123654789
  16. salmankhan200007@gmail.com:65.49.14.71:123asdzxc
  17. salmanlakho813@gmail.com:182.182.14.9:2k11swe75

cut不仅可以用分隔符来把数据分开, 还可以按照字符分开,比如,我们要提取里面第8个字符,就可以这样:

  1. cut -c 8 pass
  2. a
  3. a
  4. a
  5. a
  6. a
  7. a
  8. a
  9. a
  10. a
  11. a
  12. a
  13. a
  14. a
  15. a
  16. a
  17. a

或者第2到第8个字符:

  1. cut -c 2-8 pass
  2. hmed:sa
  3. hmed:sa
  4. hmed:sa
  5. hmed:sa
  6. hmed:sa
  7. hmed:sa
  8. hmed:sa
  9. hmed:sa
  10. hmed:sa
  11. hmed:sa
  12. hmed:sa
  13. hmed:sa
  14. hmed:sa
  15. hmed:sa
  16. hmed:sa
  17. hmed:sa

虽然上面的数据看起来没有意义, 但cut用好了会节省你很多的时间.