精品免费在线观看-精品欧美-精品欧美成人bd高清在线观看-精品欧美高清不卡在线-精品欧美日韩一区二区

17站長網(wǎng)

17站長網(wǎng) 首頁 網(wǎng)站 服務(wù)器 查看內(nèi)容

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

2022-10-24 14:32| 查看: 2038 |來源: 互聯(lián)網(wǎng)

Sort工具 Linux sort命令用于將文本文件內(nèi)容加以排序。 sort可針對文本文件的內(nèi)容,以行為單位來排序。 sort 常見選項 b 忽略每行前面開始出的空格字符。 - ...

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 顯示版本信息。
[輸入文件] 指定已排序好的文本文件。如果不指定此項,則從標準讀取數(shù)據(jù);
[輸出文件] 指定輸出的文件。如果不指定此選項,則將內(nèi)容顯示到標準輸出設(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
本文最后更新于 2022-10-24 14:32,某些文章具有時效性,若有錯誤或已失效,請在網(wǎng)站留言或聯(lián)系站長:17tui@17tui.com
·END·
站長網(wǎng)微信號:w17tui,關(guān)注站長、創(chuàng)業(yè)、關(guān)注互聯(lián)網(wǎng)人 - 互聯(lián)網(wǎng)創(chuàng)業(yè)者營銷服務(wù)中心

免責(zé)聲明:本站部分文章和圖片均來自用戶投稿和網(wǎng)絡(luò)收集,旨在傳播知識,文章和圖片版權(quán)歸原作者及原出處所有,僅供學(xué)習(xí)與參考,請勿用于商業(yè)用途,如果損害了您的權(quán)利,請聯(lián)系我們及時修正或刪除。謝謝!

17站長網(wǎng)微信二維碼

始終以前瞻性的眼光聚焦站長、創(chuàng)業(yè)、互聯(lián)網(wǎng)等領(lǐng)域,為您提供最新最全的互聯(lián)網(wǎng)資訊,幫助站長轉(zhuǎn)型升級,為互聯(lián)網(wǎng)創(chuàng)業(yè)者提供更加優(yōu)質(zhì)的創(chuàng)業(yè)信息和品牌營銷服務(wù),與站長一起進步!讓互聯(lián)網(wǎng)創(chuàng)業(yè)者不再孤獨!

掃一掃,關(guān)注站長網(wǎng)微信

大家都在看

熱門排行

    最近更新

      返回頂部
      主站蜘蛛池模板: 护士和黑人做爰 | 国产在热线精品视频国产一二 | 亚洲国产精品成人午夜在线观看 | 欧美高清免费一级在线 | 欧美日韩在线观看视频 | 国产毛片网 | 香蕉视频免费播放 | 国产午夜久久影院 | 九九视频九九热 | 午夜一区二区三区 | 欧美图片一区二区三区 | 99热久久这里只精品国产www | 成年女人免费视频播放77777 | 久久综合久久美利坚合众国 | 欧美视频一区二区专区 | sese综合| 久久精品中文字幕第一页 | 亚洲欧美日韩国产一区二区三区精品 | 久久国产大片 | 青青青国产依人在在线观看高 | 久久香蕉精品视频 | 娇喘呻吟福利视频在线观看 | 黄色影院免费观看 | 香蕉网站在线观看 | 绝对真实偷拍盗摄高清在线视频 | 亚洲美洲国产日产 | 特级毛片免费 | 噜噜噜天天躁狠狠躁夜夜精品 | 毛片1级| 黄色网页观看 | 粉嫩00福利视频在线精品 | 久久www免费人成看国产片 | 国产主播久久 | 午夜久草| 韩国r级福利视频在线观看网站 | 国产ppp在线视频在线观看 | 精品国精品国产自在久国产不卡 | 日本一级毛片免费播 | 亚洲国产日韩综合久久精品 | 欧美高清一级毛片免费视 | 福利一区二区三区视频午夜观看 |