当前位置:网站首页>RHCSA--文件內容瀏覽、cut、uniq、sort、.tr命令使用
RHCSA--文件內容瀏覽、cut、uniq、sort、.tr命令使用
2022-07-23 13:00:00 【.98℃】
目錄
1.文件內容瀏覽
1.cat命令
- 命令格式:cat [選項] 文件名列錶
- 說明:文件名可使用通配符
常用選項:
-n——對輸出內容中的所有行標注行號。
-b——對輸出內容中的非空行標注行號。
cat查看/etc/passwd文件內容,且輸出時帶行號
[[email protected] ~]# cat -n /etc/passwd[[email protected] ~]# cat -n /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
14 dbus:x:81:81:System message bus:/:/sbin/nologin
15 systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
16 systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
17 tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
18 polkitd:x:998:996:User for polkitd:/:/sbin/nologin
19 geoclue:x:997:995:User for geoclue:/var/lib/geoclue:/sbin/nologin
20 rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
21 pipewire:x:996:992:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin
22 pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
23 qemu:x:107:107:qemu user:/:/sbin/nologin
24 clevis:x:995:989:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin
25 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
26 unbound:x:994:988:Unbound DNS resolver:/etc/unbound:/sbin/nologin
27 gluster:x:993:987:GlusterFS daemons:/run/gluster:/sbin/nologin
28 rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
29 avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
30 chrony:x:992:986::/var/lib/chrony:/sbin/nologin
31 setroubleshoot:x:991:984::/var/lib/setroubleshoot:/sbin/nologin
32 saslauth:x:990:76:Saslauthd user:/run/saslauthd:/sbin/nologin
33 libstoragemgmt:x:989:983:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
34 dnsmasq:x:981:981:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
35 radvd:x:75:75:radvd user:/:/sbin/nologin
36 sssd:x:980:980:User for sssd:/:/sbin/nologin
37 cockpit-ws:x:979:979:User for cockpit web service:/nonexisting:/sbin/nologin
38 cockpit-wsinstance:x:978:978:User for cockpit-ws instances:/nonexisting:/sbin/nologin
39 flatpak:x:977:977:User for flatpak system helper:/:/sbin/nologin
40 colord:x:976:976:User for colord:/var/lib/colord:/sbin/nologin
41 rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
42 gdm:x:42:42::/var/lib/gdm:/sbin/nologin
43 gnome-initial-setup:x:975:975::/run/gnome-initial-setup/:/sbin/nologin
44 tcpdump:x:72:72::/:/sbin/nologin
45 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
46 rhcsa:x:1000:1000:rhcsa:/home/rhcsa:/bin/bash
2.more和less命令
作用:分頁查看文件內容
命令格式: more / less [選項] 文件名
常用選項:
+n ——指定從文件的第n行開始顯示。
-c ——從頂部清屏然後顯示文件內容。-數字 ——僅適用於more命令,用來指定分頁顯示時每頁的行數。
-N ——僅適用於less命令,其作用是在每行前添加輸出行號。
交互操作方法:
- 按 Enter鍵 向下逐行滾動
- 按 空格鍵 向下翻一屏、按b鍵向上翻一屏
- 文件末尾時more會自動退出,less 按q鍵退出
使用more/less查看/etc/passwd內容,且每頁顯示10行
[[email protected] ~]# more -10 /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
10
3.head和tail命令
作用:查看文件開頭或末尾的部分內容
命令:head / tail [選項] 文件名
常用選項有:
-num ——指定需要顯示文件多少行的內容,若不指定,默認只顯示十行。
-f ——使tail不停地去讀取和顯示文件最新的內容, 以監視文件內容的變化。這樣有實時監視的效果。
tail 命令更多的用於查看系統日志文件,以便於觀察重要的系統消息,特別是結合使用-f選項,tail會自動實時地把打開文件中的新消息顯示到屏幕上,從而跟踪日志文件末尾的內容變化,直至按【Ctrl+C】鍵終止顯示和跟踪。
使用head/tail分別查看文件前5行內容和後5行內容
[[email protected] ~]# head -5 /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
[[email protected] ~]# tail -5 /etc/passwd
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:975:975::/run/gnome-initial-setup/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rhcsa:x:1000:1000:rhcsa:/home/rhcsa:/bin/bash
4.grep命令
作用:檢索、過濾文件內容,在指定的文件中查找並顯示含有指定字符串的行。
格式:grep [選項] 要查找的字符串、條件錶達式或被查找的文件名
選項:
-i ——查找時忽略大小寫
-v ——反轉查找,輸出與查找條件不相符的行
在grep命令中,可以直接指定關鍵字串作為查找條件,也可以使用複雜的條件錶達式,例如:字符“^” 錶示行的開始;^read:錶示以read開始。“$” 錶示行的結尾;read$:錶示以read結束。
^$:錶示空行。
如果查找的字符串中帶有空格,可以用單引號或雙引號括起來。
使用grep查看/etc/passwd中和root相關的內容
[[email protected] ~]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
2.cut命令使用:
cut是一個選取命令,就是將一段數據經過分析(針對行),取出我們想要的。
其語法格式為:cut OPTION... [FILE]...使用說明:
- cut 命令從文件的每一行剪切字節、字符和字段並將這些字節、字符和字段寫至標准輸出。
- 如果不指定 File 參數,cut 命令將讀取標准輸入。必須指定 -b、-c 或 -f 標志之一。
主要參數:
-b —以字節為單比特進行分割。這些字節比特置將忽略多字節字符邊界,除非也指定了 -n 標志。
-c —以字符為單比特進行分割。
-d —自定義分隔符,默認為制錶符。
-f —與-d一起使用,指定顯示哪個區域。
-n —取消分割多字節字符。僅和 -b 標志一起使用。如果字符的最後一個字節落在由 -b 標志的 List 參數指示的<br />範圍之內,該字符將被寫出;否則,該字符將被排除。
給定文件cut_data.txt且內容為:
No Name Score
1 zhang 20
2 li 80
3 wang 90
4 sun 60
[[email protected] data]# cat cut_data.txt
No Name Score
1 zhang 20
2 li 80
3 wang 90
4 sun 60
使用默認定界符切割文件內容,且輸出切割後的第一個字段
[[email protected] data]# cut -d " " cut_data.txt -f1
No
1
2
3
4
切割文件內容,且輸出切割後的第一個字段和第三個字段
[[email protected] data]# cut -d " " cut_data.txt -f1,3
No Score
1 20
2 80
3 90
4 60
按字節切割:輸出切割的第一個字節到第10個字節的內容
[[email protected] data]# cut -b 1-10 cut_data.txt
No Name Sc
1 zhang 20
2 li 80
3 wang 90
4 sun 60
按字符切割:輸出切割後的第一個字符和第5個字符的內容
[[email protected] data]# cut -c 1,5 cut_data.txt
Na
1a
2
3n
4n
按指定分界符去切割:內容如下, 輸出第一個字段和第三個字段內容
No|Name|Score
1|zhang|20
2|li|80
3|wang|90
4|sun|60
[[email protected] data]# cat cut_data.txt
No|Name|Score
1|zhang|20
2|li|80
3|wang|90
4|sun|60
[[email protected] data]# cut -d "|" cut_data.txt -f1,3
No|Score
1|20
2|80
3|90
4|60
3.uniq命令使用:
uniq命令用於去除文本中連續的重複行,該命令的作用是用來去除文本文件中連續的重複行,中間不能夾雜其他文本行
語法格式為:uniq [參數] 文件名稱
option: -c:在數據行前出現的次數 -d:只打印重複的行,重複的行只顯示一次 -D:只打印重複的行,重複的行出現多少次就顯示多少次 -f:忽略行首的幾個字段 -i:忽略大小寫 -s:忽略行首的幾個字母 -u:只打印唯一的行 -w:比較不超過n個字母
新建文件uniq_data.txt,文件內容為
Welcome to Linux
Windows
Windows
Mac
Mac
Linux
[[email protected] data]# cat uniq_data.txt
Welcome to Linux
Windows
Windows
Mac
Mac
Linux
使用uniq命令輸出去重後的結果
[[email protected] data]# uniq uniq_data.txt
Welcome to Linux
Windows
Mac
Linux
使用uniq命令只輸出重複的行
[[email protected] data]# uniq -d uniq_data.txt
Windows
Mac使用uniq命令輸出不重複的行
[[email protected] data]# uniq -u uniq_data.txt
Welcome to Linux
Linux
使用uniq命令統計重複次數
[[email protected] data]# uniq -c uniq_data.txt
1 Welcome to Linux
2 Windows
2 Mac
1 Linux
4.sort命令:
sort命令用於對文本內容進行再排序
語法格式為:sort [參數] 文件名稱
參數及其作用:-f ----忽略大小寫
-b ----忽略縮進與空格
-n ----以數值型排序
-r ----反向排序
-u ----去除重複行
-t ----指定間隔符
-k ----設置字段範圍-o ----將排序結果寫入文件中
給定文件 num.txt, args.txt
文件內容:num.txt
1
3
5
2
4
[[email protected] data]# cat num.txt
1
3
5
2
4
文件內容:args.txt
test
args1
args2
args4
args4
args3
[[email protected] data]# cat args.txt
test
args1
args2
args4
args4
args3
對num.txt進行排序,且將結果輸出到sorted_num.txt中
[[email protected] data]# cat num.txt | sort -o sorted_num.txt
[[email protected] data]# cat sorted_num.txt
1
2
3
4
5
對args.txt進行排序,且將結果輸出到sorted_args.txt中
[[email protected] data]# cat args.txt | sort -o sorted_args.txt
[[email protected] data]# cat sorted_args.txt
args1
args2
args3
args4
args4
test
對num.txt和args.txt進行排序,且將結果輸出到sorted_merge.txt中
[[email protected] data]# cat args.txt num.txt | sort -o sorted_merge.txt
[[email protected] data]# cat sorted_merge.txt
1
2
3
4
5
args1
args2
args3
args4
args4
test
對args.txt排序後去重輸出
[[email protected] data]# cat args.txt | sort -u
args1
args2
args3
args4
test合並sorted_args.txt和sorted_num.txt且輸出
[[email protected] data]# cat sorted_args.txt >>sorted_num.txt
[[email protected] data]# cat sorted_num.txt
1
2
3
4
5
args1
args2
args3
args4
args4
test
給定文件info_txt:按第二列作為key進行排序
No Name Score
1 zhang 20
2 li 80
3 wang 90
4 sun 60
[[email protected] data]# sort info_txt -k2
2 li 80
No Name Score
4 sun 60
3 wang 90
1 zhang 20
5.tr
功能:轉換或删除字符。
用法:tr [OPTION]... SET1 [SET2]
選項:
-c -C, --complement 用 SET2 替換 SET1 中沒有包括的字符
-d --delete 删除 SET1 中的字符
-s --squeeze-repeats 壓縮 SET1 中重複的字符
-t --truncate-set1 將 SET1 截成與 SET2 一樣的長度
--help 幫助文檔
--version 版本信息
將26個小寫字母的後13個字母替換成大寫字母
[[email protected] data]# cat tr.txt | tr "n-z" "N-Z"
abcdefghijklmNOPQRSTUVWXYZ
將hello 123 world 456中的數字替換成空字符
[[email protected] data]# vim tr.txt1
[[email protected] data]# cat tr.txt1 | tr "1-9" " "
hello world 將hello 123 world 456中字母和空格替換掉,只保留數字
[[email protected] data]# cat tr.txt1 | tr -d " a-zA-Z"
123456
边栏推荐
- RIP 配置实例学习记录
- C #: quick sort. If there is the same number, it will be ignored, and then continue the previous search direction to find the next number that meets the requirements for replacement
- vlan配置实例学习记录
- Do a Cisco experiment!
- DHCP原理与配置
- Knowledge points and skills of Wireshark network analysis is so simple
- Unity3d:assetbundle simulation loading, synchronous loading, asynchronous loading, dependent package loading, automatic labeling, AB browser, incremental packaging
- PPP configuration instance learning record
- [untitled]
- Hcip - HDLC and PPP protocols
猜你喜欢

Single arm routing configuration instance learning record

Manually configure DHCP service

Three layer switching configuration example learning record

psutil监控的简单使用

三层交换配置实例学习记录

Learning diary - (routing and switching technology) layer 3 switch

Learning diary - (routing and switching technology) single arm routing

[Reading Notes "Phoenix architecture" - a large-scale distributed system with reliable architecture. Zhou Zhiming] (I)

Learning diary - (routing and switching technology) OSPF Protocol

Hcip - first experiment
随机推荐
vlan配置实例学习记录
Hcip --- condition matching and OSPF Protocol
[Reading Notes "Phoenix architecture" - a large-scale distributed system with reliable architecture. Zhou Zhiming] (I)
C randomly generate a score to judge its grade (excellent, good, medium, poor, failed)
学习日记——(路由与交换技术)DHCP(动态主机配置协议)
HCIA----04 路由静态扩展、VLAN
0 double pointer leetcode844. Compare strings with backspace
Quick solution: xshell can't drag into folders or software packages
0 array leetcode605. Flower planting problem
Array leetcode977. Square of ordered array
sftp部署配置
2020-09-20
查询交叉编译出的可执行文件依赖库
学习日记——(路由与交换技术)网络地址转换 NAT技术
C#输入一个字母,判断其大小写
Rk3588 compilation problem set
Hcip ---- relevant knowledge points of GRE protocol, mGRE environment and OSPF Protocol
快速解决:Xshell拖不进去文件夹或者软件包的问题
Unity3d+gameframework: resource analysis, resource dependency, circular dependency detection
Unity3d: script execution sequence on scene loading gameobejct