人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

Linux命令 sort、uniq、tr工具詳解

發(fā)布日期:2022-01-31 09:04 | 文章來源:CSDN

Sort工具

Linux sort命令用于將文本文件內(nèi)容加以排序。
sort可針對文本文件的內(nèi)容,以行為單位來排序。

sort 常見選項

b 忽略每行前面開始出的空格字符。
-c 檢查文件是否已經(jīng)按照順序排序。
-d 排序時,處理英文字母、數(shù)字及空格字符外,忽略其他的字符。
-f 排序時,將小寫字母視為大寫字母。
-i 排序時,除了040至176之間的ASCII字符外,忽略其他的字符。
-m 將幾個排序好的文件進行合并。
-M 將前面3個字母依照月份的縮寫進行排序。
-n 依照數(shù)值的大小排序。
-u 意味著是唯一的(unique),輸出的結(jié)果是去完重了的。
-o<輸出文件> 將排序后的結(jié)果存入指定的文件。
-r 以相反的順序來排序。
-t<分隔字符> 指定排序時所用的欄位分隔字符。
+<起始欄位>-<結(jié)束欄位> 以指定的欄位來排序,范圍由起始欄位到結(jié)束欄位的前一欄位。
–help 顯示幫助。
–version 顯示版本信息

sort工具示例

默認情況

默認情況下,sort工具會按字母順序進行排序

[root@1centos ~]# sort /etc/passwd
abrt:x:173:173::/etc/abrt:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
chrony:x:992:987::/var/lib/chrony:/sbin/nologin
cockpit-ws:x:990:984:User for cockpit-ws:/:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
dirsrv:x:988:982:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:981:975:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

將 /etc/passwd 以第三列進行反向排序

這邊就以數(shù)字倒敘進行排序了

[root@1centos ~]# sort -t: -rk 3 /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
ods:x:999:999:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
polkitd:x:998:997:User for polkitd:/:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
unbound:x:996:993:Unbound DNS resolver:/etc/unbound:/sbin/nologin
gluster:x:995:992:GlusterFS daemons:/run/gluster:/sbin/nologin
libstoragemgmt:x:994:991:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
saslauth:x:993:76:Saslauthd user:/run/saslauthd:/sbin/nologin
chrony:x:992:987::/var/lib/chrony:/sbin/nologin
geoclue:x:991:985:User for geoclue:/var/lib/geoclue:/sbin/nologin
cockpit-ws:x:990:984:User for cockpit-ws:/:/sbin/nologin
sssd:x:989:983:User for sssd:/:/sbin/nologin
dirsrv:x:988:982:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
setroubleshoot:x:987:981::/var/lib/setroubleshoot:/sbin/nologin
saned:x:986:980:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
gnome-initial-setup:x:985:979::/run/gnome-initial-setup/:/sbin/nologin
pcp:x:984:978:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
kdcproxy:x:983:977:IPA KDC Proxy User:/:/sbin/nologin
ipaapi:x:982:976:IPA Framework User:/:/sbin/nologin
dovenull:x:981:975:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologi
……省略……

對 /etc/passwd 的第三列進行排序,輸出到 px.txt 中

[root@1centos ~]# sort -t: -k 3 /etc/passwd -o px.txt
[root@1centos ~]# cat px.txt 
root:x:0:0:root:/root:/bin/bash
xnftp:x:1007:1007::/home/xnftp:/sbin/nologin
vuser:x:1008:1008::/opt/vuser:/sbin/nologin
tom:x:1009:1009::/home/tom:/bin/bash
jerry:x:1010:1010::/home/jerry:/bin/bash
kongkong:x:1011:1011::/home/kongkong:/bin/bash
qemu:x:107:107:qemu user:/:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

uniq工具

Linux uniq 命令用于檢查及刪除文本文件中重復(fù)出現(xiàn)的行列,一般與 sort 命令結(jié)合使用。

uniq常見選項

uniq 可檢查文本文件中重復(fù)出現(xiàn)的行列
-c或–count 在每列旁邊顯示該行重復(fù)出現(xiàn)的次數(shù)。
-d或–repeated 僅顯示重復(fù)出現(xiàn)的行列。
-f<欄位>或–skip-fields=<欄位> 忽略比較指定的欄位。
-s<字符位置>或–skip-chars=<字符位置> 忽略比較指定的字符。
-u或–unique 僅顯示出一次的行列。
-w<字符位置>或–check-chars=<字符位置> 指定要比較的字符。
–help 顯示幫助。
–version 顯示版本信息。
[輸入文件] 指定已排序好的文本文件。如果不指定此項,則從標(biāo)準(zhǔn)讀取數(shù)據(jù);
[輸出文件] 指定輸出的文件。如果不指定此選項,則將內(nèi)容顯示到標(biāo)準(zhǔn)輸出設(shè)備(顯示終端)

uniq工具示例

查看文件 the.txt

[root@1centos zhengzebiaodashi]# cat the.txt 
1the 2the 3the
1the 2the 3the
1the 2the 3the
1the 2the 3the
1the 2the 3the
2the 2the 3the
3the 2the 3the 4the 5the
4hello hi the word world
5 2 3 4 5 6 7 8

默認排序

[root@1centos zhengzebiaodashi]# uniq the.txt
1the 2the 3the
1the 2the 3the
2the 2the 3the
3the 2the 3the 4the 5the
4hello hi the word world
5 2 3 4 5 6 7 8

刪除重復(fù)行,并在顯示重復(fù)出現(xiàn)的次數(shù)

[root@1centos zhengzebiaodashi]# uniq -c the.txt 
   4 1the 2the 3the
   2 
   1 1the 2the 3the
   1 2the 2the 3the
   1 3the 2the 3the 4the 5the
   1 4hello hi the word world
   1 5 2 3 4 5 6 7 8

查找 testfile 文件中的重復(fù)行

[root@1centos zhengzebiaodashi]# uniq -d the.txt 
1the 2the 3the

tr工具

tr是translate的縮寫,是用來做翻譯或轉(zhuǎn)換工作的。具體來講,它可以對輸入內(nèi)容(stdin)進行轉(zhuǎn)換或者刪除。是linux管道的必備工具。下面來看幾個常見的用法

tr常見選項

-c:取代所有不屬于第一字符集的字符;
-d:刪除所有屬于第一字符集的字符;
-s:把連續(xù)重復(fù)的字符以單獨一個字符表示;
-t:先刪除第一字符集較第二字符集多出的字符

tr工具示例配合 echo 將展示的小寫字母變大寫

[root@1centos zhengzebiaodashi]# echo "jb51" |tr 'a-z' 'A-Z'
JB51

替換輸出的重復(fù)字符

[root@1centos zhengzebiaodashi]# echo 'Thisssssss is cdsnnn' |tr -s 'sn'
This is cdsn

刪除字符串中的默寫字符

[root@1centos zhengzebiaodashi]# echo 'this is csdn' |tr -d 'th'
is is csdn

數(shù)組排序

有了這些工具,就可以簡單的給數(shù)組進行升序或者降序了

#!/bin/bash
read -p "請輸入你的數(shù)組,是空格隔開:" a
shuzu=($a)
echo "你的數(shù)組為:${shuzu[*]}"
echo "數(shù)組升序為:"
echo "$a" |tr ' ' '\n' |sort -n |tr '\n' ' '
echo ''
echo "數(shù)組降序為:"
echo "$a" |tr ' ' '\n' |sort -nr |tr '\n' ' '
echo " "

進行使用

[root@1centos zhengzebiaodashi]# source paixu.sh 
請輸入你的數(shù)組,是空格隔開:8 1 5 9 7
你的數(shù)組為:8 1 5 9 7
數(shù)組升序為:
1 5 7 8 9 
數(shù)組降序為:
9 8 7 5 1 

到此這篇關(guān)于Linux命令 sort、uniq、tr工具詳解的文章就介紹到這了,更多相關(guān)Linux命令 sort、uniq、tr工具內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

海外服務(wù)器租用

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部