Cut 命令
cut
命令是用来切割数据的, 没错, 竖着割.
刚才搞到三蛋的库子, 那么我就拿这个数据库做例子吧 :)
首先看文件里有什么:
➤ cat pass
ahmed:[email protected]:197.44.60.223:Ahmed2542604
ahmed:[email protected]:41.196.193.71:AAHMEDEMAD944405
Ahmed:[email protected]:2.49.209.135:a13c14zI1Nwdck3a
ahmed:[email protected]:197.44.60.223:ahmed2542604
ahmed:[email protected]:216.241.47.198:hggih;fv
Ahmed:[email protected]:41.225.249.155:suffeitula.10@
ahmed:[email protected]:217.55.82.202:magicmasterz01
ahmed:[email protected]:2.89.211.62:123qwe
Ahmed:[email protected]:87.109.81.252:faraz54321
ahmed:[email protected]:217.26.255.72:asdf1234
ahmed:[email protected]:41.238.81.106:medo0125593126
ahmed:[email protected]:81.192.238.123:sonunigam1
ahmed:[email protected]:65.49.14.11:love-you2
ahmed:[email protected]:41.137.61.65:azerty123654789
ahmed:[email protected]:65.49.14.71:123asdzxc
ahmed:[email protected]:182.182.14.9:2k11swe75
好, 里面是以 用户名:邮箱:IP地址:密码
这种格式存储的, 那么,如果我们要想提取处里面所有的密码该怎么办呢?
这就是cut
的威力了:
➤ cut -d : -f 4 pass
Ahmed2542604
AAHMEDEMAD944405
a13c14zI1Nwdck3a
ahmed2542604
hggih;fv
suffeitula.10@
magicmasterz01
123qwe
faraz54321
asdf1234
medo0125593126
sonunigam1
love-you2
azerty123654789
123asdzxc
2k11swe75
➤
详细解释一下这条命令, 其中-d
参数后面跟的是分隔符, 我们看到, 里面的数据都是用:
分隔的, 所以用-d :
把每一列分开, 然后的-f
代表第几列, f
也就是filed
的意思. 由于密码是在第4个字段, 所以这里是-f 4
.
提取用户名+密码呢?
➤ cut -d : -f 1,4 pass #用','分隔我们要选取的字段
ahmed:Ahmed2542604
ahmed:AAHMEDEMAD944405
Ahmed:a13c14zI1Nwdck3a
ahmed:ahmed2542604
ahmed:hggih;fv
Ahmed:suffeitula.10@
ahmed:magicmasterz01
ahmed:123qwe
Ahmed:faraz54321
ahmed:asdf1234
ahmed:medo0125593126
ahmed:sonunigam1
ahmed:love-you2
ahmed:azerty123654789
ahmed:123asdzxc
ahmed:2k11swe75
提取邮箱后面的呢?
➤ cut -d : -f 2- pass #用'-'表示从哪儿到哪儿,如果不填则表示最后(或者最前)
[email protected]:197.44.60.223:Ahmed2542604
[email protected]:41.196.193.71:AAHMEDEMAD944405
[email protected]:2.49.209.135:a13c14zI1Nwdck3a
[email protected]:197.44.60.223:ahmed2542604
[email protected]:216.241.47.198:hggih;fv
[email protected]:41.225.249.155:suffeitula.10@
[email protected]:217.55.82.202:magicmasterz01
[email protected]:2.89.211.62:123qwe
[email protected]:87.109.81.252:faraz54321
[email protected]:217.26.255.72:asdf1234
[email protected]:41.238.81.106:medo0125593126
[email protected]:81.192.238.123:sonunigam1
[email protected]:65.49.14.11:love-you2
[email protected]:41.137.61.65:azerty123654789
[email protected]:65.49.14.71:123asdzxc
[email protected]:182.182.14.9:2k11swe75
cut
不仅可以用分隔符来把数据分开, 还可以按照字符分开,比如,我们要提取里面第8个字符,就可以这样:
➤ cut -c 8 pass
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
➤
或者第2到第8个字符:
➤ cut -c 2-8 pass
hmed:sa
hmed:sa
hmed:sa
hmed:sa
hmed:sa
hmed:sa
hmed:sa
hmed:sa
hmed:sa
hmed:sa
hmed:sa
hmed:sa
hmed:sa
hmed:sa
hmed:sa
hmed:sa
➤
虽然上面的数据看起来没有意义, 但cut
用好了会节省你很多的时间.