Linux中cut命令的基本使用詳解
Linux系統(tǒng)之cut命令的基本使用
一、cut命令介紹
1. cut命令簡(jiǎn)介
cut命令是一個(gè)Linux/Unix命令,用于從文件或標(biāo)準(zhǔn)輸入中提取字段并輸出到標(biāo)準(zhǔn)輸出。cut 經(jīng)常用來(lái)顯示文件的內(nèi)容,顯示行中的指定部分,刪除文件中指定字段。
2.cut命令的由來(lái)
- cut命令是一個(gè)基于Unix和類Unix系統(tǒng)的命令行工具,用于從文件或輸入流中提取文本。
- cut命令的名稱來(lái)自于其“cut out”或“cut off”字面意思,它指的是從文本中“剪切”部分內(nèi)容。
- cut命令最初是由貝爾實(shí)驗(yàn)室的Ken Thompson開(kāi)發(fā)的,它于1971年首次出現(xiàn)在Unix的早期版本中。
- 該命令的設(shè)計(jì)旨在使用戶能夠快速輕松地從文本中提取所需的數(shù)據(jù),而不必手動(dòng)搜索和刪除不必要的內(nèi)容。這使得cut命令成為Unix和類Unix系統(tǒng)中最常用的命令之一。
二、在Linux中命令幫助
1. cut的help幫助信息
在Linux命令行中,cut的help幫助信息。
[root@server-01 ~]# cut --help Usage: cut OPTION... [FILE]... Print selected parts of lines from each FILE to standard output. Mandatory arguments to long options are mandatory for short options too. -b, --bytes=LIST select only these bytes -c, --characters=LIST select only these characters -d, --delimiter=DELIM use DELIM instead of TAB for field delimiter -f, --fields=LIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified -n with -b: don't split multibyte characters --complement complement the set of selected bytes, characters or fields -s, --only-delimited do not print lines not containing delimiters --output-delimiter=STRING use STRING as the output delimiter the default is to use the input delimiter --help display this help and exit --version output version information and exit Use one, and only one of -b, -c or -f. Each LIST is made up of one range, or many ranges separated by commas. Selected input is written in the same order that it is read, and is written exactly once. Each range is one of: N N'th byte, character or field, counted from 1 N- from N'th byte, character or field, to end of line N-M from N'th to M'th (included) byte, character or field -M from first to M'th (included) byte, character or field With no FILE, or when FILE is -, read standard input. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> For complete documentation, run: info coreutils 'cut invocation'
2. cut的選項(xiàng)
cut命令的選項(xiàng)解釋
-b:僅顯示行中指定直接范圍的內(nèi)容;
-c:僅顯示行中指定范圍的字符;
-d:指定字段的分隔符,默認(rèn)的字段分隔符為“TAB”;
-f:顯示指定字段的內(nèi)容;
-n:與“-b”選項(xiàng)連用,不分割多字節(jié)字符;
--complement:補(bǔ)足被選擇的字節(jié)、字符或字段;
--out-delimiter= 字段分隔符:指定輸出內(nèi)容是的字段分割符;
--help:顯示指令的幫助信息;
--version:顯示指令的版本信息。
三、cut的基本使用
1. 指定字段的內(nèi)容
使用-f 選項(xiàng),打印文本的列,例如打印文本的第一列內(nèi)容,使用-f 1。
[root@server-01 ~]# cut -f 1 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin nscd:x:28:28:NSCD Daemon:/:/sbin/nologin admin:x:1000:1000::/home/admin:/bin/bash saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin mongod:x:996:993:mongod:/var/lib/mongo:/bin/false mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
2. 使用分隔符打印文本內(nèi)容
cut命令的默認(rèn)分隔符是制表符(tab鍵),也就是"\t"??梢允褂脜?shù)-d來(lái)指定其他分隔符,例如使用逗號(hào)作為分隔符:cut -d ‘,’ filename。
[root@server-01 ~]# cut -d ":" -f 1 /etc/passwd root bin daemon adm lp sync shutdown halt mail operator games ftp nobody systemd-network dbus polkitd sshd postfix chrony ntp tcpdump nscd admin saslauth mongod mysql
[root@server-01 ~]# cut -d ":" -f 1,2 /etc/passwd root:x bin:x daemon:x adm:x lp:x sync:x shutdown:x halt:x mail:x operator:x games:x ftp:x nobody:x systemd-network:x dbus:x polkitd:x sshd:x postfix:x chrony:x ntp:x tcpdump:x nscd:x admin:x saslauth:x mongod:x mysql:x
3. 顯示行中指定范圍的字符
cut 命令可以將一串字符作為列來(lái)顯示,
字符字段的記法:
N- :從第 N 個(gè)字節(jié)、字符、字段到結(jié)尾;
N-M :從第 N 個(gè)字節(jié)、字符、字段到第 M 個(gè)(包括 M 在內(nèi))字節(jié)、字符、字段;
-M :從第 1 個(gè)字節(jié)、字符、字段到第 M 個(gè)(包括 M 在內(nèi))字節(jié)、字符、字段。
[root@server-01 ~]# cut -c1-2 /etc/passwd ro bi da ad lp sy sh ha ma op ga ft no sy db po ss po ch nt tc ns ad sa mo my
四、cut命令的日常使用
1. 提取IP地址
使用cut提取本地網(wǎng)卡地址
[root@server001 ~]# ifconfig eth0 |grep -w inet |cut -d ' ' -f 10 192.168.3.157
使用awk提取本地網(wǎng)卡地址
[root@server001 ~]# ifconfig eth0 |grep netmask |awk '{print $2}' 192.168.3.157
2. 提取本地系統(tǒng)的用戶名
在/etc/passwd文件中打印本地系統(tǒng)的用戶名
[root@server001 ~]# cut -d ":" -f 1 /etc/passwd root bin daemon adm lp sync shutdown halt mail operator games ftp nobody systemd-network dbus polkitd sshd postfix admin tss postgres redis www-data mysql zabbix apache cockpit-ws chrony geoclue gluster libstoragemgmt rpc
3. 統(tǒng)計(jì)本地用戶數(shù)
查看系統(tǒng)本地的用戶有多少個(gè)。
[root@server001 ~]# cut -d ":" -f 1 /etc/passwd |wc -l 32
4. 打印文本最后的5個(gè)字符
打印文本最后的5個(gè)字符
[root@server001 ~]# cat /etc/passwd | rev | cut -c -5 | rev /bash login login login login /sync tdown /halt login login login login login login login login login login /bash login /bash login /bash false login login login login login login login login
到此這篇關(guān)于Linux系統(tǒng)之cut命令的基本使用的文章就介紹到這了,更多相關(guān)Linux cut命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux makefile 和shell文件相互調(diào)用實(shí)例詳解
這篇文章主要介紹了Linux makefile 和shell文件相互調(diào)用實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03用shell腳本和c語(yǔ)言將大寫(xiě)字母轉(zhuǎn)成小寫(xiě)的代碼
最近在學(xué)shell編程,在網(wǎng)上看到tr '[:upper:]' '[:lower:]' 可以把字符串中大寫(xiě)字母轉(zhuǎn)換成小寫(xiě)字母,我就在這個(gè)的基礎(chǔ)上寫(xiě)了一個(gè)腳本來(lái)自己學(xué)習(xí)學(xué)習(xí)2013-08-08Shell腳本統(tǒng)計(jì)當(dāng)前目錄下目錄和文件的數(shù)量
這篇文章主要介紹了Shell腳本統(tǒng)計(jì)當(dāng)前目錄下目錄和文件的數(shù)量,Linux下如何統(tǒng)計(jì)當(dāng)前目錄下文件有多少個(gè),目錄又有多少個(gè)呢,使用本文腳本即可實(shí)現(xiàn),需要的朋友可以參考下2014-12-12Linux查看系統(tǒng)時(shí)間的詳細(xì)方法總結(jié)
系統(tǒng)時(shí)間是計(jì)算機(jī)硬件和軟件運(yùn)行的基礎(chǔ),在Linux系統(tǒng)中,查看系統(tǒng)時(shí)間是一項(xiàng)基本任務(wù),本文將從多個(gè)方面介紹Linux查看系統(tǒng)時(shí)間的詳細(xì)方法,需要的朋友可以參考下2023-09-09linux每天定時(shí)備份數(shù)據(jù)庫(kù)并刪除十天前數(shù)據(jù)詳細(xì)步驟
每天定時(shí)備份數(shù)據(jù)庫(kù)需要用到Linux的定時(shí)任務(wù),利用Linux的crondtab 命令。下面通過(guò)本文給大家?guī)?lái)了linux每天定時(shí)備份數(shù)據(jù)庫(kù)并刪除十天前數(shù)據(jù)詳細(xì)步驟,感興趣的朋友一起看看吧2018-06-06分享一個(gè)實(shí)用的iptables腳本(各種過(guò)濾寫(xiě)法參考)
這篇文章主要介紹了分享一個(gè)實(shí)用的iptables腳本(各種過(guò)濾寫(xiě)法參考),需要的朋友可以參考下2014-04-04簡(jiǎn)單的遠(yuǎn)程FTP定時(shí)備份Shell腳本分享
這篇文章主要介紹了簡(jiǎn)單的遠(yuǎn)程FTP定時(shí)備份Shell腳本分享,,需要的朋友可以參考下2014-07-07Linux下is not in the sudoers file的解決
當(dāng)我們使用sudo命令切換用戶的時(shí)候可能會(huì)遇到提示以下錯(cuò)誤:用戶名 is not in the sudoers file.本文給大家分享原因分析及解決方案,感興趣的朋友跟隨小編一起看看吧2023-02-02