[華為云在線課程][Linux文本處理工具和正則表達式][第二章文本常見處理工具][學習筆記]
文件內容查看命令

查看文本文件內容
cat
cat可以查看文本內容
格式:
cat [OPTIONS]... [FILE]...
常見選項:
-E 顯示行結束符$ -A 顯示所有控制符 -n 對顯示出的每一行進行編號 -b 非空行編號 -s 壓縮連續的空行成一行
例子:
[root@localhost Code]# cat -E 99.sh #!/bin/bash$ $ for i in {1..9};do$ for j in `seq $i`;do$ echo -ne "${j}x${i}=$((i*j))\t" $ done$ echo$ done$ [root@localhost Code]# cat -n 99.sh 1 #!/bin/bash 2 3 for i in {1..9};do 4 for j in `seq $i`;do 5 echo -ne "${j}x${i}=$((i*j))\t" 6 done 7 echo 8 done [root@localhost Code]# cat -b 99.sh 1 #!/bin/bash 2 for i in {1..9};do 3 for j in `seq $i`;do 4 echo -ne "${j}x${i}=$((i*j))\t" 5 done 6 echo 7 done
nl
顯示行號,相當于cat -b
[root@localhost Code]# nl 99.sh 1 #!/bin/bash 2 for i in {1..9};do 3 for j in `seq $i`;do 4 echo -ne "${j}x${i}=$((i*j))\t" 5 done 6 echo 7 done
tac
逆向顯示文本內容
[root@localhost Code]# tac 99.sh done echo done echo -ne "${j}x${i}=$((i*j))\t" for j in `seq $i`;do for i in {1..9};do #!/bin/bash [root@localhost Code]# seq 3 1 2 3 [root@localhost Code]# seq 3|tac 3 2 1
rev
將同一行的內容逆向顯示
[root@localhost Code]# cat hello.txt hello world 123 456 789 [root@localhost Code]# tac hello.txt 789 456 123 world hello [root@localhost Code]# rev hello.txt olleh dlrow 321 654 987 [root@localhost Code]# rev abcdef fedcba [root@localhost Code]# echo {1..10} 1 2 3 4 5 6 7 8 9 10 [root@localhost Code]# echo {1..10}|rev 01 9 8 7 6 5 4 3 2 1
查看非文本文件的內容
hexdump
例子:
[root@localhost Code]# hexdump -C -n 512 hello.txt 00000000 68 65 6c 6c 6f 0a 77 6f 72 6c 64 0a 31 32 33 0a |hello.world.123.| 00000010 34 35 36 0a 37 38 39 0a |456.789.| 00000018
od
od即dump files in octal and other formats
例子:
[root@localhost Code]# echo {a..z} | tr -d ' ' | od -t x 0000000 64636261 68676665 6c6b6a69 706f6e6d 0000020 74737271 78777675 000a7a79 0000033 [root@localhost Code]# echo {a..z} | tr -d ' ' | od -t x1 0000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 0000020 71 72 73 74 75 76 77 78 79 7a 0a 0000033 [root@localhost Code]# echo {a..z} | tr -d ' ' | od -t x1z 0000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 >abcdefghijklmnop< 0000020 71 72 73 74 75 76 77 78 79 7a 0a >qrstuvwxyz.< 0000033
xxd
[root@localhost Code]# echo {a..z} | tr -d ' ' | xxd 0000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop 0000010: 7172 7374 7576 7778 797a 0a qrstuvwxyz.
分頁查看文件內容
more
可以實現分頁查看文件,可以配合管道實現輸出信息的分頁
格式
more [OPTIONS...] FILE...
選項:
-d:顯示翻頁及退出提示
less
less,也可以實現分頁查看文件或STDIN輸出,less命令是man命令使用的分頁器
查看時有用的命令包括:
/文本 搜索 文本 n/N 跳到下一個 或 上一個匹配
例子:
[root@localhost Code]# cat /etc/init.d/functions |less
顯示文本前或后行內容
head
可以顯示文件或標準輸入的前面行
格式:
head [OPTIONS]... [FILE]...
選項:
-c # 指定獲取前#字節 -n # 指定獲取前#行 -# 同上
例子:
[root@localhost Code]# head -n 2 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin [root@localhost Code]# head -2 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin [root@localhost Code]# echo helloworld | head -c5 hello[root@localhost Code]#
# 生成隨機密碼 [root@localhost ~]# cat /dev/urandom | tr -dc '[:alnum:]' | head -c5 rnNTa[root@localhost ~]# # 生成隨機密碼并保存到pass.txt中 [root@localhost ~]# cat /dev/urandom | tr -dc '[:alnum:]' | head -c5 | tee pass.txt GbfNT[root@localhost ~]#
[root@localhost Code]# head -n -3 hello.txt hello world [root@localhost Code]# head -n -4 hello.txt hello [root@localhost Code]# cat hello.txt hello world 123 456 789
tail
tail和head相反,查看文件或標準輸入的倒數行
格式:
tail [OPTION]... [FILE]...
常用選擇:
-c # 指定獲取后#字節 -n # 指定獲取后#行 -# 同上 -f 跟蹤顯示文件fd新追加的內容,常用日志監控,相當于--follow=descriptor,當文件刪除再新建同名文件,將無法繼續跟蹤文件 -F 跟蹤文件名,相當于--follow=name --retry,當文件刪除再新建同名文件,將可以繼續跟蹤文件 tailf 類似tail -f,當文件不增長時并不訪問文件
例子:
[root@localhost Code]# tail -n 3 hello.txt 123 456 789 [root@localhost Code]# tail -n +3 hello.txt 123 456 789 [root@localhost Code]# tail -n 5 hello.txt hello world 123 456 789
head和tail總結
按列抽取文本cut
cut命令可以提取文本文件或STDIN數據的指定列
格式
cut [OPTION]... [FILE]...
常用選項:
-d DELEMITER 指明分隔符,默認tab -f FILEDS: #:第#個字段,例如:3 #,#[,#]:離散的多個字段,例如:1,3,6 #-#:連續的多個字段,例如:1-6 混合使用:1-3,7 -c 按字符切割 --output-delimiter=STRING 指定輸出分隔符
例子:截取文本
# 原始數據 [root@localhost ~]# cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin # 按列抽取文本的1,3到4,7列,以:作為分隔符 [root@localhost Code]# cut -d: -f1,3-4,7 /etc/passwd root:0:0:/bin/bash bin:1:1:/sbin/nologin
例子:截取ip
[root@localhost ~]# ifconfig ens33: flags=4163
例子:截取磁盤利用率
[root@localhost ~]# df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 914484 0 914484 0% /dev tmpfs 931516 0 931516 0% /dev/shm tmpfs 931516 10392 921124 2% /run tmpfs 931516 0 931516 0% /sys/fs/cgroup /dev/mapper/centos-root 17811456 4673740 13137716 27% / /dev/sda1 1038336 189120 849216 19% /boot tmpfs 186304 12 186292 1% /run/user/42 tmpfs 186304 0 186304 0% /run/user/0
[root@localhost Code]# df | tr -s " " Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 914484 0 914484 0% /dev tmpfs 931516 0 931516 0% /dev/shm tmpfs 931516 10392 921124 2% /run tmpfs 931516 0 931516 0% /sys/fs/cgroup /dev/mapper/centos-root 17811456 4673752 13137704 27% / /dev/sda1 1038336 189120 849216 19% /boot tmpfs 186304 12 186292 1% /run/user/42 tmpfs 186304 0 186304 0% /run/user/0 [root@localhost Code]# df | tr -s " "|cut -d" " -f5 Use% 0% 0% 2% 0% 27% 19% 1% 0% [root@localhost Code]# df | tr -s " "|cut -d" " -f5|tr -dc "[0-9\n]" 0 0 2 0 27 19 1 0
[root@localhost Code]# df | tr -s " " % Filesystem%1K-blocks%Used%Available%Use%Mounted%on devtmpfs%914484%0%914484%0%/dev tmpfs%931516%0%931516%0%/dev/shm tmpfs%931516%10392%921124%2%/run tmpfs%931516%0%931516%0%/sys/fs/cgroup /dev/mapper/centos-root%17811456%4673984%13137472%27%/ /dev/sda1%1038336%189120%849216%19%/boot tmpfs%186304%12%186292%1%/run/user/42 tmpfs%186304%0%186304%0%/run/user/0 [root@localhost Code]# df | tr -s " " % | cut -d% -f5 Use 0 0 2 0 27 19 1 0 [root@localhost Code]# df | tr -s " " % | cut -d% -f5|tr -d "[:alpha:]" 0 0 2 0 27 19 1 0
[root@localhost Code]# df | cut -c54-55 se 0 0 2 0 27 19 1 0 [root@localhost Code]# df | cut -c54-55 | tr -d "[:alpha:]" 0 0 2 0 27 19 1 0
[root@localhost Code]# cut -d: -f1,3,7 --output-delimiter="===" /etc/passwd | head -3 root===0===/bin/bash bin===1===/sbin/nologin daemon===2===/sbin/nologin
[root@localhost Code]# df | tr -s " " Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 914484 0 914484 0% /dev tmpfs 931516 0 931516 0% /dev/shm tmpfs 931516 10424 921092 2% /run tmpfs 931516 0 931516 0% /sys/fs/cgroup /dev/mapper/centos-root 17811456 4665720 13145736 27% / /dev/sda1 1038336 189120 849216 19% /boot tmpfs 186304 12 186292 1% /run/user/42 tmpfs 186304 0 186304 0% /run/user/0 [root@localhost Code]# df | tr -s " "|cut -d" " -f5 Use% 0% 0% 2% 0% 27% 19% 1% 0% [root@localhost Code]# df | tr -s " "|cut -d" " -f5|tr -d % Use 0 0 2 0 27 19 1 0
[root@localhost Code]# df | tr -s " " "%" Filesystem%1K-blocks%Used%Available%Use%Mounted%on devtmpfs%914484%0%914484%0%/dev tmpfs%931516%0%931516%0%/dev/shm tmpfs%931516%10424%921092%2%/run tmpfs%931516%0%931516%0%/sys/fs/cgroup /dev/mapper/centos-root%17811456%4665720%13145736%27%/ /dev/sda1%1038336%189120%849216%19%/boot tmpfs%186304%12%186292%1%/run/user/42 tmpfs%186304%0%186304%0%/run/user/0 [root@localhost Code]# df | tr -s " " "%"|cut -d% -f5 Use 0 0 2 0 27 19 1 0
合并多個文件paste
paste合并多個文件同行號的列到一行
格式
paste [OPTION]... [FILE]...
常用選項:
-d 分隔符:指定分隔符,默認用TAB -s 所有行合成一行顯示
例子:
[root@localhost Code]# cat alpha.log a b c d e f g [root@localhost Code]# cat seq.log 1 2 3 4 5 6 7 [root@localhost Code]# cat alpha.log seq.log a b c d e f g 1 2 3 4 5 6 7 [root@localhost Code]# paste alpha.log seq.log a 1 b 2 c 3 d 4 e 5 f 6 g 7 [root@localhost Code]# paste -d":" alpha.log seq.log a:1 b:2 c:3 d:4 e:5 f:6 g:7 [root@localhost Code]# paste -s seq.log 1 2 3 4 5 6 7 [root@localhost Code]# paste -s alpha.log a b c d e f g [root@localhost Code]# paste -s alpha.log seq.log a b c d e f g 1 2 3 4 5 6 7
例子:批量修改密碼
[root@localhost Code]# paste -d: user.txt pass.txt hello:1234 world:5678 [root@localhost Code]# paste -d: user.txt pass.txt |chpasswd
分析文本的工具
文本數據統計:wc
整理文本:sort
比較文件:diff和patch
收集文本統計數據wc
wc命令可用于統計文件的總行數、單詞總數、字節總數和字符總數
可以對文件或STDIN中的數據統計
常用選項
-l 只計數行數 -w 只計數單詞總數 -c 只計數字節總數 -m 只計數字符總數 -L 顯示文件中最長行的長度
例子:
[root@localhost Code]# wc hello.txt 5 5 24 hello.txt 行數 單詞數 字節數 [root@localhost Code]# cat hello.txt hello world 123 456 789
例子:單詞文件
[root@localhost Code]# wc -l /usr/share/dict/linux.words 479828 /usr/share/dict/linux.words
文本排序sort
把整理過的文本顯示在STDOUT,不改變原始文件
格式:
sort [options] file(s)
常用選項
-r 執行反方向(由上至下)整理 -R 隨機排序 -n 執行按數字大小整理 -h 人類可讀排序,如:2K 1G -f 選項忽略(fold)字符串中的字符大小寫 -u 選項(獨特,unique),合并重復項,即去重 -t c 選項使用c作為字段界定符 -k # 選項按照使用c字符分隔的#列來整理能夠使用多次
例子:
[root@localhost ~]# cut -d: -f1,3 /etc/passwd | head -n3 root:0 bin:1 daemon:2 [root@localhost ~]# cut -d: -f1,3 /etc/passwd | head -n3|sort -t: -k2 -nr daemon:2 bin:1 root:0
例子:統計分區利用率
Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 914484 0 914484 0% /dev tmpfs 931516 0 931516 0% /dev/shm tmpfs 931516 10788 920728 2% /run tmpfs 931516 0 931516 0% /sys/fs/cgroup /dev/mapper/centos-root 17811456 4667636 13143820 27% / /dev/sda1 1038336 189120 849216 19% /boot tmpfs 186304 0 186304 0% /run/user/0 tmpfs 186304 24 186280 1% /run/user/1000 # 查看分區利用率最高值 [root@localhost ~]# df | tr -s " " "%"|cut -d% -f5|sort -nr|head -1 27 [root@localhost ~]# df | tr -s " " "%"|cut -d% -f5|tr -d '[:alpha:]'|sort 0 0 0 0 1 19 2 27 [root@localhost ~]# df | tr -s " " "%"|cut -d% -f5|tr -d '[:alpha:]'|sort -n|tail -n1 27
面試題:有兩個文件,a.txt和b.txt,合并兩個文件,并輸出時確保每個數字也唯一
# a.txt中的每一個數字在本文件唯一 [root@localhost Code]# cat a.txt 12 34 56 78 90 # b.txt中的每一個數字在本文件唯一 [root@localhost Code]# cat b.txt 56 78 90 11 22 33 # 將兩個文件合并后重復的行消除,不保留 [root@localhost Code]# cat a.txt b.txt |sort -u 11 12 22 33 34 56 78 90
去重uniq
uniq命令從輸入中刪除前后相接的重復的行
格式:
uniq [OPTION]... [FILE]...
常見選項:
-c 顯示每行重復出現的次數 -d 僅顯示重復過的行 -u 僅顯示不曾重復的行
uniq常和sort命令一起配合使用:
sort user.txt | uniq -c
例子:取兩個文件的相同和不同的行
[root@localhost Code]# cat a.txt 12 34 56 78 90 [root@localhost Code]# cat b.txt 56 78 90 11 22 33 # 取文件的共同行 [root@localhost Code]# cat a.txt b.txt | sort | uniq -d 56 78 90 # 取文件的不同行 [root@localhost Code]# cat a.txt b.txt | sort | uniq -u 11 12 22 33 34
比較文件
diff
diff命令比較兩個文件之間的區別
-u 選項來輸出"統一的(unified)"diff格式文件,最適用于補丁文件
例子:
[root@localhost Code]# cat a.txt 12 34 56 78 90 [root@localhost Code]# cat b.txt 56 78 90 11 22 33 [root@localhost Code]# diff a.txt b.txt 1,2d0 < 12 < 34 5a4,6 > 11 > 22 > 33 [root@localhost Code]# diff a.txt b.txt -c *** a.txt 2022-04-12 07:12:44.273846443 +0800 #表示第一個文件 --- b.txt 2022-04-12 07:13:32.557843941 +0800 #表示第二個文件 *************** *** 1,5 **** - 12 #表示第一個文件獨有 - 34 56 78 90 --- 1,6 ---- 56 78 90 + 11 #表示第二個文件獨有 + 22 + 33 [root@localhost Code]# diff a.txt b.txt -u --- a.txt 2022-04-12 07:12:44.273846443 +0800 +++ b.txt 2022-04-12 07:13:32.557843941 +0800 @@ -1,5 +1,6 @@ -12 -34 56 78 90 +11 +22 +33
patch
patch 復制在其他文件中進行的改變(要謹慎使用)
-b 選項來自動備份改變了的文件
例子:
diff -u foo.conf foo2.conf > foo.patch patch -b foo.conf foo.patch
vimdiff
相當于vim -d
[root@localhost Code]# cat a.txt 12 34 56 78 90 [root@localhost Code]# cat b.txt 56 78 90 11 22 33 [root@localhost Code]# which vimdiff /usr/bin/vimdiff [root@localhost Code]# ll /usr/bin/vimdiff lrwxrwxrwx. 1 root root 3 Apr 4 13:40 /usr/bin/vimdiff -> vim [root@localhost Code]# vimdiff a.txt b.txt 2 files to edit [root@localhost Code]# cat a.txt 12 34 56 78 90 [root@localhost Code]# cat b.txt 56 78 90 11 22 33
cmp
例子:查看二進制文件的不同
[root@localhost Code]# ll /usr/bin/dir /usr/bin/ls -rwxr-xr-x. 1 root root 117608 Aug 20 2019 /usr/bin/dir -rwxr-xr-x. 1 root root 117608 Aug 20 2019 /usr/bin/ls [root@localhost Code]# ll /usr/bin/dir /usr/bin/ls -i 50724194 -rwxr-xr-x. 1 root root 117608 Aug 20 2019 /usr/bin/dir 50724215 -rwxr-xr-x. 1 root root 117608 Aug 20 2019 /usr/bin/ls [root@localhost Code]# diff /usr/bin/dir /usr/bin/ls -i Binary files /usr/bin/dir and /usr/bin/ls differ [root@localhost Code]# cmp /bin/dir /bin/ls /bin/dir /bin/ls differ: byte 645, line 1 [root@localhost Code]# hexdump -s 730 -Cn 7 /bin/dir 000002da 00 00 00 00 00 00 00 |.......| 000002e1 [root@localhost Code]# hexdump -s 730 -Cn 7 /bin/ls 000002da 00 00 00 00 00 00 00 |.......| 000002e1
Linux
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。