Linux中cut命令的基本使用詳解
Linux系統(tǒng)之cut命令的基本使用
一、cut命令介紹
1. cut命令簡介
cut命令是一個Linux/Unix命令,用于從文件或標準輸入中提取字段并輸出到標準輸出。cut 經常用來顯示文件的內容,顯示行中的指定部分,刪除文件中指定字段。
2.cut命令的由來
- cut命令是一個基于Unix和類Unix系統(tǒng)的命令行工具,用于從文件或輸入流中提取文本。
- cut命令的名稱來自于其“cut out”或“cut off”字面意思,它指的是從文本中“剪切”部分內容。
- cut命令最初是由貝爾實驗室的Ken Thompson開發(fā)的,它于1971年首次出現(xiàn)在Unix的早期版本中。
- 該命令的設計旨在使用戶能夠快速輕松地從文本中提取所需的數(shù)據,而不必手動搜索和刪除不必要的內容。這使得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的選項
cut命令的選項解釋
-b:僅顯示行中指定直接范圍的內容;
-c:僅顯示行中指定范圍的字符;
-d:指定字段的分隔符,默認的字段分隔符為“TAB”;
-f:顯示指定字段的內容;
-n:與“-b”選項連用,不分割多字節(jié)字符;
--complement:補足被選擇的字節(jié)、字符或字段;
--out-delimiter= 字段分隔符:指定輸出內容是的字段分割符;
--help:顯示指令的幫助信息;
--version:顯示指令的版本信息。
三、cut的基本使用
1. 指定字段的內容
使用-f 選項,打印文本的列,例如打印文本的第一列內容,使用-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. 使用分隔符打印文本內容
cut命令的默認分隔符是制表符(tab鍵),也就是"\t"。可以使用參數(shù)-d來指定其他分隔符,例如使用逗號作為分隔符: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 命令可以將一串字符作為列來顯示,
字符字段的記法:
N- :從第 N 個字節(jié)、字符、字段到結尾;
N-M :從第 N 個字節(jié)、字符、字段到第 M 個(包括 M 在內)字節(jié)、字符、字段;
-M :從第 1 個字節(jié)、字符、字段到第 M 個(包括 M 在內)字節(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提取本地網卡地址
[root@server001 ~]# ifconfig eth0 |grep -w inet |cut -d ' ' -f 10 192.168.3.157
使用awk提取本地網卡地址
[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)計本地用戶數(shù)
查看系統(tǒng)本地的用戶有多少個。
[root@server001 ~]# cut -d ":" -f 1 /etc/passwd |wc -l 32
4. 打印文本最后的5個字符
打印文本最后的5個字符
[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
到此這篇關于Linux系統(tǒng)之cut命令的基本使用的文章就介紹到這了,更多相關Linux cut命令內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Linux makefile 和shell文件相互調用實例詳解
這篇文章主要介紹了Linux makefile 和shell文件相互調用實例詳解的相關資料,需要的朋友可以參考下2017-03-03Shell腳本統(tǒng)計當前目錄下目錄和文件的數(shù)量
這篇文章主要介紹了Shell腳本統(tǒng)計當前目錄下目錄和文件的數(shù)量,Linux下如何統(tǒng)計當前目錄下文件有多少個,目錄又有多少個呢,使用本文腳本即可實現(xiàn),需要的朋友可以參考下2014-12-12linux每天定時備份數(shù)據庫并刪除十天前數(shù)據詳細步驟
每天定時備份數(shù)據庫需要用到Linux的定時任務,利用Linux的crondtab 命令。下面通過本文給大家?guī)砹薼inux每天定時備份數(shù)據庫并刪除十天前數(shù)據詳細步驟,感興趣的朋友一起看看吧2018-06-06Linux下is not in the sudoers file的解決
當我們使用sudo命令切換用戶的時候可能會遇到提示以下錯誤:用戶名 is not in the sudoers file.本文給大家分享原因分析及解決方案,感興趣的朋友跟隨小編一起看看吧2023-02-02