日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Linux cat more grep head tail cut uniq sort tr命令詳解

作者:程序員Fy 更新時間: 2022-07-19 編程語言

目錄

  • cat命令
    • 1 cat在查看文件內容時同時輸出行號應該怎么做?
  • 2 more 命令
  • 3 grep命令
  • 4 head , tail 命令
    • 1 head 查看內容
    • 2 tail 查看內容
  • cut命令
    • 1 要求剪切后輸出第一個字段
    • 2 輸出第一字段和第三字段內容
  • uniq命令
    • 1 使用uniq命令輸出去重后的結果
    • 2 使用uniq命令只輸出重復的行
    • 3 使用uniq命令統(tǒng)計重復的次數(shù)
  • sort命令
    • 1 對num.txt排序,并且將結果輸出到sorted_num.txt中
    • 2 對args.txt排序,并且將結果輸出到sorted_args.txt中
    • 3 對args.txt和num.txt排序,并將結果輸出到sorted_merge.txt中
    • 4 對args.txt排序后去重輸出合并sorted_args.txt和sorted_num.txt且輸出給定文件info.txt按第二列作為key進行排序
  • tr命令
    • 1 將后14個英文字母變成大寫
    • 2 將文件中數(shù)字變?yōu)榭兆址?/li>
  • 結束

cat命令

1 cat在查看文件內容時同時輸出行號應該怎么做?

如下:查看/etc/passwd/下的內容并且輸出行號
注意:/etc/passwd/ 這個文件下不是關于密碼的配置文件,而是關乎于用戶的配置文件。
命令:

[root@localhost Desktop]# cat -n /etc/passwd

2 more 命令

例:還是查看/etc/passwd目錄下的內容:
more命令

[root@localhost Desktop]# more -p10 /etc/passwd

3 grep命令

例: 查看/etc/passwd 中和root相關的內容:
命令:

[root@localhost Desktop]# grep -G "root" /etc/passwd

4 head , tail 命令

1 head 查看內容

例:查看 /etc/passwd 內容的前5行內容:
命令:

[root@localhost Desktop]# head -n 5 /etc/passwd

默認是輸出10行

2 tail 查看內容

例:查看 /etc/passwd 內容的前5行內容:
命令:

[root@localhost Desktop]# tail -n 5 /etc/passwd

默認輸出也是10行。

cut命令

首先創(chuàng)建一個文件:cut_data.txt ,且內容為:

No	Name	Score
1		zhang	20
2		li			80
2      wang    90
4       sun     60

1 要求剪切后輸出第一個字段

命令:

[root@localhost Desktop]# cut -b 1-2 cut_data

結果如下:

在這里插入圖片描述
按字節(jié)切割,輸出切割后的第一個字節(jié)到第10個字節(jié)的內容:

[root@localhost Desktop]# cut -b 1-10 cut_data

按字符切割,輸出切割后的第一個字符到第5個字符的內容:

[root@localhost Desktop]# cut -c 1-5 cut_data 

將上面文件內容改為:

No|Name|Score
1|zhang|20
2|li|80
2|wang|90
4|sun|60

2 輸出第一字段和第三字段內容

說明:因為這里是以 | 作為分隔的所以在用分隔條件時需要加上轉義符 ** ,所以是 -d| 。整個命令解釋是:將cut_data整個文件的內容作為標準輸入到cut語句中,以 | 為分界符(可知分成了3個域),剪切出第1和3域。
命令和輸出如下:

[root@localhost Desktop]# cat cut_data | cut -d\| -f 1,3
No|Score
1|20
2|80
2|90
4|60

uniq命令

新建文件uniq_data.txt 并且內容寫:

Welcome to Linux
Windows
Windows
Mac
Mac
Linux

1 使用uniq命令輸出去重后的結果

命令及輸出結果:

[root@localhost Desktop]# uniq uniq_data.txt 
Welcome to Linux
Windows
Mac
Linux

注意:默認是去重

2 使用uniq命令只輸出重復的行

命令及輸出結果:

[root@localhost Desktop]# uniq -d uniq_data.txt 
Windows
Mac

3 使用uniq命令統(tǒng)計重復的次數(shù)

命令及結果:

[root@localhost Desktop]# uniq -c uniq_data.txt 
  1 Welcome to Linux
  2 Windows
  2 Mac
  1 Linux

注意數(shù)字代表重復個數(shù)

sort命令

例:給定文件num.txt, args.txt
內容:
num.txt:

 1
3
5
2
4

注意第一行有一個空格。

args.txt內容:

args1
args2
args4
args4
args3

1 對num.txt排序,并且將結果輸出到sorted_num.txt中

首先創(chuàng)建文件寫入內容不用說了。
命令及結果:

[root@localhost Desktop]# sort -n num.txt > sorted_num.txt
 1
2
3
4
5

注意: -b 參數(shù)是不管開頭的空白,但這里不加這個參數(shù)好像也行。

2 對args.txt排序,并且將結果輸出到sorted_args.txt中

命令及顯示結果:

[root@localhost Desktop]# cat sorted_args.txt 
[root@localhost Desktop]# cat sorted_args.txt 
args1
args2
args3
args4
args4

3 對args.txt和num.txt排序,并將結果輸出到sorted_merge.txt中

命令及結果:

[root@localhost Desktop]# sort -n args.txt num.txt > sorted_merge.txt
[root@localhost Desktop]# cat sorted_merge.txt 
args1
args2
args3
args4
args4
 1
2
3
4
5

4 對args.txt排序后去重輸出合并sorted_args.txt和sorted_num.txt且輸出給定文件info.txt按第二列作為key進行排序

命令及結果:

[root@localhost Desktop]# sort args.txt | uniq | cat sorted_args.txt sorted_num.txt > info.txt | sort -k 2

注意:這個命令本人不確定對不對,應該與描述的命令一樣

tr命令

1 將后14個英文字母變成大寫

如下命令及結果:

[root@localhost Desktop]# tr m-z M-Z
abcdefghijklmnopqrstuvwxyz
abcdefghijklMNOPQRSTUVWXYZ

2 將文件中數(shù)字變?yōu)榭兆址?/h2>

aaa.txt中寫入:

hello 123 world 456

命令及結果:

[root@localhost Desktop]# cat aaa.txt | tr 0-9 " "-" "
hello     world 

注意:" “-” " 。 ""號中間有個空格

結束

原文鏈接:https://blog.csdn.net/fuyuo/article/details/125823897

欄目分類
最近更新