利用linux命令行進行文本按行去重并按重復(fù)次數(shù)排序linux命令行提供了非常強大的文本處理功能,組合利用linux命令能實現(xiàn)好多強大的功能。本文這里舉例說明如何利用linux命令行進行文本按行去重并按重復(fù)次數(shù)排序。主要用到的命令有sort,uniq和cut。其中,sort主要功能是排序,uniq主要功能是實現(xiàn)相鄰文本行的去重,cut可以從文本行中提取相應(yīng)的文本列(簡單地說,就是按列操作文本行)。用于演示的測試文件內(nèi)容如下:[plain]Hello
創(chuàng)新互聯(lián)建站主營盧氏網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都App制作,盧氏h5小程序制作搭建,盧氏網(wǎng)站營銷推廣歡迎盧氏等地區(qū)企業(yè)咨詢
World.
Apple
and
Nokia.
Hello
World.
I
wanna
buy
an
Apple
device.
The
Iphone
of
Apple
company.
Hello
World.
The
Iphone
of
Apple
company.
My
name
is
Friendfish.
Hello
World.
Apple
and
Nokia.
實現(xiàn)命令及過程如下:[plain]1、文本行去重
(1)排序
由于uniq命令只能對相鄰行進行去重復(fù)操作,所以在進行去重前,先要對文本行進行排序,使重復(fù)行集中到一起。
$
sort
test.txt
Apple
and
Nokia.
Apple
and
Nokia.
Hello
World.
Hello
World.
Hello
World.
Hello
World.
I
wanna
buy
an
Apple
device.
My
name
is
Friendfish.
The
Iphone
of
Apple
company.
The
Iphone
of
Apple
company.
(2)去掉相鄰的重復(fù)行
$
sort
test.txt
|
uniq
Apple
and
Nokia.
Hello
World.
I
wanna
buy
an
Apple
device.
My
name
is
Friendfish.
The
Iphone
of
Apple
company.
2、文本行去重并按重復(fù)次數(shù)排序
(1)首先,對文本行進行去重并統(tǒng)計重復(fù)次數(shù)(uniq命令加-c選項可以實現(xiàn)對重復(fù)次數(shù)進行統(tǒng)計。)。
$
sort
test.txt
|
uniq
-c
2
Apple
and
Nokia.
4
Hello
World.
1
I
wanna
buy
an
Apple
device.
1
My
name
is
Friendfish.
2
The
Iphone
of
Apple
company.
(2)對文本行按重復(fù)次數(shù)進行排序。
sort
-n可以識別每行開頭的數(shù)字,并按其大小對文本行進行排序。默認(rèn)是按升序排列,如果想要按降序要加-r選項(sort
-rn)。
$
sort
test.txt
|
uniq
-c
|
sort
-rn
4
Hello
World.
2
The
Iphone
of
Apple
company.
2
Apple
and
Nokia.
1
My
name
is
Friendfish.
1
I
wanna
buy
an
Apple
device.
(3)每行前面的刪除重復(fù)次數(shù)。
cut命令可以按列操作文本行??梢钥闯銮懊娴闹貜?fù)次數(shù)占8個字符,因此,可以用命令cut
-c
9-
取出每行第9個及其以后的字符。
$
sort
test.txt
|
uniq
-c
|
sort
-rn
|
cut
-c
9-
Hello
World.
The
Iphone
of
Apple
company.
Apple
and
Nokia.
My
name
is
Friendfish.
I
wanna
buy
an
Apple
device.
下面附帶說一下cut命令的使用,用法如下:[plain]cut
-b
list
[-n]
[file
...]
cut
-c
list
[file
...]
cut
-f
list
[-d
delim][-s][file
...]
上面的-b、-c、-f分別表示字節(jié)、字符、字段(即byte、character、field);
list表示-b、-c、-f操作范圍,-n常常表示具體數(shù)字;
file表示的自然是要操作的文本文件的名稱;
delim(英文全寫:delimiter)表示分隔符,默認(rèn)情況下為TAB;
-s表示不包括那些不含分隔符的行(這樣有利于去掉注釋和標(biāo)題)
三種方式中,表示從指定的范圍中提取字節(jié)(-b)、或字符(-c)、或字段(-f)。
范圍的表示方法:
n
只有第n項
n-
從第n項一直到行尾
n-m
從第n項到第m項(包括m)
-m
從一行的開始到第m項(包括m)
-
從一行的開始到結(jié)束的所有項
在寫這篇文章的時候,用到了vim的大小寫轉(zhuǎn)化的快捷鍵:gu變小寫,gU變大寫。結(jié)合ctrl+v能夠?qū)⒁黄淖种械淖址M行大小寫轉(zhuǎn)換,非常好用。
方法一
for i in {1..10}; do echo "Hello, World";
方法二
在~/.bashrc文件中創(chuàng)建一個run函數(shù):
function run() {
number=$1
shift
for n in $(seq $number); do
$@
done
}
1
2
3
4
5
6
7
使./bashrc生效
souce ~/./bashrc
1
示例
run 10 echo "Hello, World"
如何重復(fù)執(zhí)行?
使用循環(huán)語句可以控制循環(huán)次數(shù)
當(dāng)然你可以把那幾條命令打包成一個函數(shù)
可以反復(fù)執(zhí)行
也可以循環(huán)控制重復(fù)執(zhí)行shell腳本
當(dāng)然也可以定時執(zhí)行
網(wǎng)站欄目:linux如何重復(fù)命令 linux自動重復(fù)命令
當(dāng)前鏈接:http://jinyejixie.com/article0/dohpgio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、企業(yè)網(wǎng)站制作、網(wǎng)站設(shè)計公司、網(wǎng)站制作、網(wǎng)站內(nèi)鏈、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)